/** * Allocate a new counter with a given label with a default type of {@link #DEFAULT_TYPE_ID}. * * @param label to describe the counter. * @return the id allocated for the counter. */ public int allocate(final String label) { return allocate(label, DEFAULT_TYPE_ID); }
public int allocate(final String label, final int typeId) { lock.lock(); try { return super.allocate(label, typeId); } finally { lock.unlock(); } }
public int allocate(final String label, final int typeId, final Consumer<MutableDirectBuffer> keyFunc) { lock.lock(); try { return super.allocate(label, typeId, keyFunc); } finally { lock.unlock(); } }
/** * Allocate a counter record and wrap it with a new {@link AtomicCounter} for use. * * @param label to describe the counter. * @param typeId for the type of counter. * @return a newly allocated {@link AtomicCounter} */ public AtomicCounter newCounter(final String label, final int typeId) { return new AtomicCounter(valuesBuffer, allocate(label, typeId), this); }
/** * Allocate a counter record and wrap it with a new {@link AtomicCounter} for use with a default type * of {@link #DEFAULT_TYPE_ID}. * * @param label to describe the counter. * @return a newly allocated {@link AtomicCounter} */ public AtomicCounter newCounter(final String label) { return new AtomicCounter(valuesBuffer, allocate(label), this); }
/** * Allocate a counter record and wrap it with a new {@link AtomicCounter} for use. * * @param label to describe the counter. * @param typeId for the type of counter. * @param keyFunc for setting the key value for the counter. * @return a newly allocated {@link AtomicCounter} */ public AtomicCounter newCounter(final String label, final int typeId, final Consumer<MutableDirectBuffer> keyFunc) { return new AtomicCounter(valuesBuffer, allocate(label, typeId, keyFunc), this); }
try return super.allocate(typeId, keyBuffer, keyOffset, keyLength, labelBuffer, labelOffset, labelLength);
@Test public void shouldFreeAndReuseCountersAfterCooldown() { final int abc = managerWithCooldown.allocate("abc"); final int def = managerWithCooldown.allocate("def"); final int ghi = managerWithCooldown.allocate("ghi"); managerWithCooldown.free(def); currentTimestamp += FREE_TO_REUSE_TIMEOUT; assertThat(managerWithCooldown.allocate("the next label"), is(def)); }
@Test public void shouldFreeAndNotReuseCountersThatHaveCooldown() { final int abc = managerWithCooldown.allocate("abc"); final int def = managerWithCooldown.allocate("def"); final int ghi = managerWithCooldown.allocate("ghi"); managerWithCooldown.free(def); currentTimestamp += FREE_TO_REUSE_TIMEOUT - 1; assertThat(managerWithCooldown.allocate("the next label"), is(greaterThan(ghi))); }
@Test public void shouldStoreMultipleLabels() { final int abc = manager.allocate("abc"); final int def = manager.allocate("def"); final int ghi = manager.allocate("ghi"); reader.forEach(consumer); final InOrder inOrder = Mockito.inOrder(consumer); inOrder.verify(consumer).accept(abc, "abc"); inOrder.verify(consumer).accept(def, "def"); inOrder.verify(consumer).accept(ghi, "ghi"); inOrder.verifyNoMoreInteractions(); }
@Test public void shouldTruncateLongLabel() { final int labelLength = MAX_LABEL_LENGTH + 10; final StringBuilder sb = new StringBuilder(labelLength); for (int i = 0; i < labelLength; i++) { sb.append('x'); } final String label = sb.toString(); final int counterId = manager.allocate(label); reader.forEach(consumer); verify(consumer).accept(counterId, label.substring(0, MAX_LABEL_LENGTH)); }
@Test public void shouldFreeAndReuseCounters() { final int abc = manager.allocate("abc"); final int def = manager.allocate("def"); final int ghi = manager.allocate("ghi"); manager.free(def); reader.forEach(consumer); final InOrder inOrder = Mockito.inOrder(consumer); inOrder.verify(consumer).accept(abc, "abc"); inOrder.verify(consumer).accept(ghi, "ghi"); inOrder.verifyNoMoreInteractions(); assertThat(manager.allocate("the next label"), is(def)); }
@Test public void shouldStoreLabels() { final int counterId = manager.allocate("abc"); reader.forEach(consumer); verify(consumer).accept(counterId, "abc"); }
public static int allocateCounterId( final MutableDirectBuffer tempBuffer, final String name, final int typeId, final CountersManager countersManager, final long registrationId) { tempBuffer.putLong(REGISTRATION_ID_OFFSET, registrationId); final int keyLength = REGISTRATION_ID_OFFSET + SIZE_OF_LONG; final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT); int labelLength = 0; labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, name); labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, ": "); labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, registrationId); return countersManager.allocate( typeId, tempBuffer, 0, keyLength, tempBuffer, labelOffset, labelLength); } }
@Test public void shouldStoreMetaData() { final int typeIdOne = 333; final long keyOne = 777L; final int typeIdTwo = 222; final long keyTwo = 444; final int counterIdOne = manager.allocate("Test Label One", typeIdOne, (buffer) -> buffer.putLong(0, keyOne)); final int counterIdTwo = manager.allocate("Test Label Two", typeIdTwo, (buffer) -> buffer.putLong(0, keyTwo)); manager.forEach(metaData); final ArgumentCaptor<DirectBuffer> argCaptorOne = ArgumentCaptor.forClass(DirectBuffer.class); final ArgumentCaptor<DirectBuffer> argCaptorTwo = ArgumentCaptor.forClass(DirectBuffer.class); final InOrder inOrder = Mockito.inOrder(metaData); inOrder.verify(metaData).accept(eq(counterIdOne), eq(typeIdOne), argCaptorOne.capture(), eq("Test Label One")); inOrder.verify(metaData).accept(eq(counterIdTwo), eq(typeIdTwo), argCaptorTwo.capture(), eq("Test Label Two")); inOrder.verifyNoMoreInteractions(); final DirectBuffer keyOneBuffer = argCaptorOne.getValue(); assertThat(keyOneBuffer.getLong(0), is(keyOne)); final DirectBuffer keyTwoBuffer = argCaptorTwo.getValue(); assertThat(keyTwoBuffer.getLong(0), is(keyTwo)); }
@Test public void shouldStoreAndLoadValue() { final int counterId = manager.allocate("Test Counter"); final long value = 7L; manager.setCounterValue(counterId, value); assertThat(manager.getCounterValue(counterId), is(value)); } }
@Test public void shouldStoreRawData() { final int typeIdOne = 333; final long keyOne = 777L; final MutableDirectBuffer keyOneBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(8)); keyOneBuffer.putLong(0, keyOne); final DirectBuffer labelOneBuffer = new UnsafeBuffer("Test Label One".getBytes(US_ASCII)); final int typeIdTwo = 222; final long keyTwo = 444; final MutableDirectBuffer keyTwoBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(8)); keyTwoBuffer.putLong(0, keyTwo); final DirectBuffer labelTwoBuffer = new UnsafeBuffer("Test Label Two".getBytes(US_ASCII)); final int counterIdOne = manager.allocate( typeIdOne, keyOneBuffer, 0, keyOneBuffer.capacity(), labelOneBuffer, 0, labelOneBuffer.capacity()); final int counterIdTwo = manager.allocate( typeIdTwo, keyTwoBuffer, 0, keyTwoBuffer.capacity(), labelTwoBuffer, 0, labelTwoBuffer.capacity()); manager.forEach(metaData); final ArgumentCaptor<DirectBuffer> argCaptorOne = ArgumentCaptor.forClass(DirectBuffer.class); final ArgumentCaptor<DirectBuffer> argCaptorTwo = ArgumentCaptor.forClass(DirectBuffer.class); final InOrder inOrder = Mockito.inOrder(metaData); inOrder.verify(metaData).accept(eq(counterIdOne), eq(typeIdOne), argCaptorOne.capture(), eq("Test Label One")); inOrder.verify(metaData).accept(eq(counterIdTwo), eq(typeIdTwo), argCaptorTwo.capture(), eq("Test Label Two")); inOrder.verifyNoMoreInteractions(); final DirectBuffer keyOneBufferCapture = argCaptorOne.getValue(); assertThat(keyOneBufferCapture.getLong(0), is(keyOne)); final DirectBuffer keyTwoBufferCapture = argCaptorTwo.getValue(); assertThat(keyTwoBufferCapture.getLong(0), is(keyTwo)); }
@Test public void shouldCopeWithExceptionKeyFunc() { final RuntimeException ex = new RuntimeException(); try { manager.allocate( "label", CountersManager.DEFAULT_TYPE_ID, (buffer) -> { throw ex; }); } catch (final RuntimeException caught) { assertThat(caught, is(ex)); final AtomicCounter counter = manager.newCounter("new label"); assertThat(counter.id(), is(0)); return; } fail("Should have thrown exception"); }
labelOffset + labelLength, channel, 0, MAX_LABEL_LENGTH - labelLength); return countersManager.allocate( typeId, tempBuffer,