Skip to content

Commit

Permalink
Merge pull request #1169 from OSGP/feature/high_prio_pool_disable
Browse files Browse the repository at this point in the history
  • Loading branch information
kroesctrl authored Jan 30, 2024
2 parents 2aecb7e + c60d0e0 commit 82ea360
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ public boolean releasePermit(

final AtomicInteger permitCounter = this.getPermitCounter(baseTransceiverStationId, cellId);

// Notify that permit is released
synchronized (permitCounter) {
permitCounter.notifyAll();
}

final int numberOfPermitsIfReleased = permitCounter.decrementAndGet();
if (numberOfPermitsIfReleased < 0) {
permitCounter.incrementAndGet();
Expand All @@ -134,9 +129,20 @@ public boolean releasePermit(
this.permitRepository.releasePermit(
throttlingConfigId, clientId, baseTransceiverStationId, cellId, requestId);

if (this.useHighPrioPool()) {
// Notify that permit is released
synchronized (permitCounter) {
permitCounter.notifyAll();
}
}

return numberOfReleasedPermits == 1;
}

private boolean useHighPrioPool() {
return this.maxWaitForHighPrioInMs != 0;
}

private boolean isPermitAvailable(
final int baseTransceiverStationId,
final int cellId,
Expand All @@ -148,6 +154,10 @@ private boolean isPermitAvailable(
if (numberOfPermitsIfGranted > maxConcurrency) {
permitCounter.decrementAndGet();

if (!this.useHighPrioPool()) {
return false;
}

if (priority <= MessagePriorityEnum.DEFAULT.getPriority()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ scheduling.task.cleanup.permits.cron.expression=0 0/30 * * * ?
# Releasing expired permits will happen in batches of the following size.
cleanup.permits.batch.size=100

max.wait.for.high.prio.in.ms=5000
#max.wait.for.high.prio.in.ms=5000
# For now we disable this high prio pool by default
max.wait.for.high.prio.in.ms=0

# The task to reset in memory counters with db state is executed by cron expression.
scheduling.task.reinitialize.state.cron.expression=30 0/30 * * * ?
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensmartgridplatform.throttling.repositories.PermitRepository;
Expand Down Expand Up @@ -130,6 +136,78 @@ void testInitializeDelete() {
assertThat(this.permitsPerNetworkSegment.permitsPerNetworkSegment()).isEmpty();
}

@ParameterizedTest
@ValueSource(ints = {0, 2000})
void testHighPrioPoolTime(final int maxWaitForHighPrio) {
this.permitsPerNetworkSegment =
new PermitsPerNetworkSegment(this.permitRepository, maxWaitForHighPrio);

final int btsId = 1;
final int cellId = 2;
final int numberOfPermits = 3;
final short throttlingConfigId = Integer.valueOf(1).shortValue();
final int clientId = 4;
final int requestId = 5;
final int priority = 6;
final int maxConcurrency = numberOfPermits;

this.preparePermits(btsId, cellId, numberOfPermits, throttlingConfigId);

this.permitsPerNetworkSegment.initialize(throttlingConfigId);

final long start = System.currentTimeMillis();
final boolean permitGranted =
this.permitsPerNetworkSegment.requestPermit(
throttlingConfigId, clientId, btsId, cellId, requestId, priority, maxConcurrency);
assertThat(permitGranted).isFalse();
assertThat(System.currentTimeMillis() - start).isGreaterThanOrEqualTo(maxWaitForHighPrio);

verify(this.permitRepository, never())
.grantPermit(throttlingConfigId, clientId, btsId, cellId, requestId);
}

@Test
void testHighPrioPool() {
final int maxWaitForHighPrio = 10000;
final int waitBeforeRelease = 1000;
this.permitsPerNetworkSegment =
new PermitsPerNetworkSegment(this.permitRepository, maxWaitForHighPrio);

final int btsId = 1;
final int cellId = 2;
final int numberOfPermits = 3;
final short throttlingConfigId = Integer.valueOf(1).shortValue();
final int clientId = 4;
final int requestId = 5;
final int priority = 6;
final int maxConcurrency = numberOfPermits;

this.preparePermits(btsId, cellId, numberOfPermits, throttlingConfigId);

when(this.permitRepository.grantPermit(throttlingConfigId, clientId, btsId, cellId, requestId))
.thenReturn(true);

this.permitsPerNetworkSegment.initialize(throttlingConfigId);

final long start = System.currentTimeMillis();

final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.schedule(
() -> {
PermitsPerNetworkSegmentTest.this.permitsPerNetworkSegment.releasePermit(
throttlingConfigId, clientId, btsId, cellId, requestId);
},
waitBeforeRelease,
TimeUnit.MILLISECONDS);

final boolean permitGranted =
this.permitsPerNetworkSegment.requestPermit(
throttlingConfigId, clientId, btsId, cellId, requestId, priority, maxConcurrency);
assertThat(permitGranted).isTrue();
assertThat((int) (System.currentTimeMillis() - start))
.isBetween(waitBeforeRelease, maxWaitForHighPrio);
}

private void preparePermits(
final int btsId,
final int cellId,
Expand Down

0 comments on commit 82ea360

Please sign in to comment.