use of org.infinispan.counter.api.StrongCounter in project infinispan by infinispan.
the class StrongCounterImplTestStrategy method testBoundaries.
@Override
public void testBoundaries(Method method) {
final String counterName = method.getName();
final CounterManager counterManager = counterManagerSupplier.get();
assertTrue(counterManager.defineCounter(counterName, builder(BOUNDED_STRONG).initialValue(1).lowerBound(0).upperBound(20).build()));
StrongCounter counter = counterManager.getStrongCounter(counterName);
assertCounterValue(counter, 1);
expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.addAndGet(-10).get());
expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.addAndGet(30).get());
assertCounterValue(counter, 20);
expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.compareAndSet(20, -1).get());
assertCounterValue(counter, 20);
expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.compareAndSet(20, 21).get());
}
use of org.infinispan.counter.api.StrongCounter in project infinispan by infinispan.
the class TestCounterManager method getStrongCounter.
@Override
public StrongCounter getStrongCounter(String name) {
StrongCounter c = (StrongCounter) counters.get(name);
if (c == null) {
CounterConfiguration config = getConfiguration(name);
if (config == null || config.type() == CounterType.WEAK) {
throw new IllegalStateException();
}
c = new TestStrongCounter(name, config, client, notificationManager);
counters.put(name, c);
}
return c;
}
use of org.infinispan.counter.api.StrongCounter in project indy by Commonjava.
the class ScheduleDB method init.
@PostConstruct
public void init() {
String keyspace = config.getScheduleKeyspace();
session = client.getSession(keyspace);
session.execute(SchemaUtils.getSchemaCreateKeyspace(keyspace, indyConfig.getKeyspaceReplicas()));
session.execute(ScheduleDBUtil.getSchemaCreateTableSchedule(keyspace));
session.execute(ScheduleDBUtil.getSchemaCreateTypeIndex4Schedule(keyspace));
session.execute(ScheduleDBUtil.getSchemaCreateTableExpiration(keyspace));
MappingManager manager = new MappingManager(session);
scheduleMapper = manager.mapper(DtxSchedule.class, keyspace);
expirationMapper = manager.mapper(DtxExpiration.class, keyspace);
preparedExpiredQuery = session.prepare("SELECT scheduleuid, expirationtime, storekey, jobname FROM " + keyspace + "." + ScheduleDBUtil.TABLE_EXPIRATION + " WHERE expirationpid = ?");
preparedSingleScheduleQuery = session.prepare("SELECT storekey, jobtype, jobname, scheduletime, scheduleuid, payload, lifespan, expired FROM " + keyspace + "." + ScheduleDBUtil.TABLE_SCHEDULE + " WHERE storekey = ? and jobname = ?");
preparedExpiredUpdate = session.prepare("UPDATE " + keyspace + "." + ScheduleDBUtil.TABLE_SCHEDULE + " SET expired = true WHERE storekey = ? and jobname = ?");
preparedScheduleByTypeQuery = session.prepare("SELECT storekey, jobtype, jobname, scheduletime, scheduleuid, payload, lifespan, expired FROM " + keyspace + "." + ScheduleDBUtil.TABLE_SCHEDULE + " WHERE jobtype = ? ");
preparedScheduleByStoreKeyAndTypeQuery = session.prepare("SELECT storekey, jobtype, jobname, scheduletime, scheduleuid, payload, lifespan, expired FROM " + keyspace + "." + ScheduleDBUtil.TABLE_SCHEDULE + " WHERE storekey = ? and jobtype = ?");
preparedScheduleByStoreKeyQuery = session.prepare("SELECT storekey, jobtype, jobname, scheduletime, scheduleuid, payload, lifespan, expired FROM " + keyspace + "." + ScheduleDBUtil.TABLE_SCHEDULE + " WHERE storekey = ? ");
StrongCounter remoteCounter = cacheProducer.getStrongCounter("scheduleCounter");
AtomicLong localCounter = new AtomicLong(0);
if (remoteCounter != null) {
try {
localCounter.set(remoteCounter.getValue().get());
} catch (InterruptedException | ExecutionException e) {
logger.warn(" Get the value of the counter error. ", e);
}
}
service.scheduleAtFixedRate(() -> {
if (remoteCounter != null) {
try {
long expect = localCounter.get();
long update = localCounter.incrementAndGet();
// otherwise we think that other indy nodes had executed the schedule checking during this period.
if (remoteCounter.compareAndSwap(expect, update).get() != expect) {
localCounter.set(remoteCounter.getValue().get());
return;
}
} catch (InterruptedException | ExecutionException e) {
logger.warn("Checking the counter error. ", e);
}
}
// When the hour shift, the service need to check the remaining jobs expired in the last minutes of the last hour.
LocalDateTime localDateTime = LocalDateTime.now();
if (localDateTime.getMinute() <= config.getScheduleRatePeriod() / 60) {
LocalDateTime offsetDateTime = localDateTime.minusHours(config.getOffsetHours());
queryAndSetExpiredSchedule(Date.from(offsetDateTime.atZone(ZoneId.systemDefault()).toInstant()));
}
queryAndSetExpiredSchedule(Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
}, 10, config.getScheduleRatePeriod(), TimeUnit.SECONDS);
}
use of org.infinispan.counter.api.StrongCounter in project quarkus by quarkusio.
the class TestServlet method incrementCounter.
@Path("incr/{id}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public CompletionStage<Long> incrementCounter(@PathParam("id") String id) {
ensureStart();
CounterConfiguration configuration = counterManager.getConfiguration(id);
if (configuration == null) {
configuration = CounterConfiguration.builder(CounterType.BOUNDED_STRONG).build();
counterManager.defineCounter(id, configuration);
}
StrongCounter strongCounter = counterManager.getStrongCounter(id);
return strongCounter.incrementAndGet();
}
use of org.infinispan.counter.api.StrongCounter in project infinispan by infinispan.
the class BoundedCounterNotificationTest method testThreshold.
public void testThreshold(Method method) throws Exception {
final String counterName = method.getName();
final StrongCounter[] counters = new StrongCounter[clusterSize()];
for (int i = 0; i < clusterSize(); ++i) {
counters[i] = createCounter(counterManager(i), counterName, -2, 2);
}
Handle<ListenerQueue> l = counters[0].addListener(new ListenerQueue(counterName));
if (counters.length != CLUSTER_SIZE) {
for (int i = 0; i < CLUSTER_SIZE; ++i) {
counters[0].incrementAndGet();
}
} else {
for (StrongCounter counter : counters) {
counter.incrementAndGet();
}
}
l.getCounterListener().assertEvent(0, CounterState.VALID, 1, CounterState.VALID);
l.getCounterListener().assertEvent(1, CounterState.VALID, 2, CounterState.VALID);
l.getCounterListener().assertEvent(2, CounterState.VALID, 2, CounterState.UPPER_BOUND_REACHED);
// as soon as threshold is reached, no more events are triggered.
assertTrue(l.getCounterListener().queue.isEmpty());
eventuallyEquals(2L, () -> counters[0].sync().getValue());
if (counters.length != CLUSTER_SIZE) {
for (int i = 0; i < CLUSTER_SIZE; ++i) {
counters[0].decrementAndGet();
}
} else {
for (StrongCounter counter : counters) {
counter.decrementAndGet();
}
}
l.getCounterListener().assertEvent(2, CounterState.UPPER_BOUND_REACHED, 1, CounterState.VALID);
l.getCounterListener().assertEvent(1, CounterState.VALID, 0, CounterState.VALID);
l.getCounterListener().assertEvent(0, CounterState.VALID, -1, CounterState.VALID);
l.getCounterListener().assertEvent(-1, CounterState.VALID, -2, CounterState.VALID);
eventuallyEquals(-2L, () -> counters[0].sync().getValue());
counters[0].decrementAndGet();
counters[0].decrementAndGet();
l.getCounterListener().assertEvent(-2, CounterState.VALID, -2, CounterState.LOWER_BOUND_REACHED);
// as soon as threshold is reached, no more events are triggered.
assertTrue(l.getCounterListener().queue.isEmpty());
eventuallyEquals(-2L, () -> counters[0].sync().getValue());
// removes the listener
l.remove();
counters[0].incrementAndGet();
counters[0].incrementAndGet();
assertTrue(l.getCounterListener().queue.isEmpty());
}
Aggregations