/** * Free the counter identified by counterId. * * @param counterId the counter to freed */ public void free(final int counterId) { final int recordOffset = metaDataOffset(counterId); metaDataBuffer.putLong( recordOffset + FREE_FOR_REUSE_DEADLINE_OFFSET, epochClock.time() + freeToReuseTimeoutMs); metaDataBuffer.putIntOrdered(recordOffset, RECORD_RECLAIMED); freeList.addInt(counterId); }
private int nextCounterId() { final long nowMs = epochClock.time(); for (int i = 0, size = freeList.size(); i < size; i++) { final int counterId = freeList.getInt(i); final long deadlineMs = metaDataBuffer.getLongVolatile( metaDataOffset(counterId) + FREE_FOR_REUSE_DEADLINE_OFFSET); if (nowMs >= deadlineMs) { freeList.remove(i); valuesBuffer.putLongOrdered(counterOffset(counterId), 0L); return counterId; } } return ++idHighWaterMark; }
checkCountersCapacity(counterId); final int recordOffset = metaDataOffset(counterId); checkMetaDataCapacity(recordOffset);
/** * Allocate a new counter with a given label and type. * * @param label to describe the counter. * @param typeId for the type of counter. * @return the id allocated for the counter. */ public int allocate(final String label, final int typeId) { final int counterId = nextCounterId(); checkCountersCapacity(counterId); final int recordOffset = metaDataOffset(counterId); checkMetaDataCapacity(recordOffset); try { metaDataBuffer.putInt(recordOffset + TYPE_ID_OFFSET, typeId); metaDataBuffer.putLong(recordOffset + FREE_FOR_REUSE_DEADLINE_OFFSET, NOT_FREE_TO_REUSE); putLabel(recordOffset, label); metaDataBuffer.putIntOrdered(recordOffset, RECORD_ALLOCATED); } catch (final Exception ex) { freeList.pushInt(counterId); LangUtil.rethrowUnchecked(ex); } return counterId; }
checkCountersCapacity(counterId); final int recordOffset = metaDataOffset(counterId); checkMetaDataCapacity(recordOffset);
/** * Free the counter identified by counterId. * * @param counterId the counter to freed */ public void free(final int counterId) { metaDataBuffer.putIntOrdered(metaDataOffset(counterId), RECORD_RECLAIMED); freeList.pushInt(counterId); }
/** * Free the counter identified by counterId. * * @param counterId the counter to freed */ public void free(final int counterId) { final int recordOffset = metaDataOffset(counterId); metaDataBuffer.putLong( recordOffset + FREE_FOR_REUSE_DEADLINE_OFFSET, epochClock.time() + freeToReuseTimeoutMs); metaDataBuffer.putIntOrdered(recordOffset, RECORD_RECLAIMED); freeList.addInt(counterId); }
/** * Allocate a new counter with a given label. * * The key function will be called with a buffer with the exact length of available key space * in the record for the user to store what they want for the key. No offset is required. * * @param label to describe the counter. * @param typeId for the type of counter. * @param keyFunc for setting the key value for the counter. * @return the id allocated for the counter. */ public int allocate(final String label, final int typeId, final Consumer<MutableDirectBuffer> keyFunc) { final int counterId = nextCounterId(); if ((counterOffset(counterId) + COUNTER_LENGTH) > valuesBuffer.capacity()) { throw new IllegalArgumentException("Unable to allocated counter, values buffer is full"); } final int recordOffset = metaDataOffset(counterId); if ((recordOffset + METADATA_LENGTH) > metaDataBuffer.capacity()) { throw new IllegalArgumentException("Unable to allocate counter, labels buffer is full"); } metaDataBuffer.putInt(recordOffset + TYPE_ID_OFFSET, typeId); keyFunc.accept(new UnsafeBuffer(metaDataBuffer, recordOffset + KEY_OFFSET, MAX_KEY_LENGTH)); metaDataBuffer.putStringUtf8(recordOffset + LABEL_OFFSET, label, MAX_LABEL_LENGTH); metaDataBuffer.putIntOrdered(recordOffset, RECORD_ALLOCATED); return counterId; }
private int nextCounterId() { final long nowMs = epochClock.time(); for (int i = 0, size = freeList.size(); i < size; i++) { final int counterId = freeList.getInt(i); final long deadlineMs = metaDataBuffer.getLongVolatile( metaDataOffset(counterId) + FREE_FOR_REUSE_DEADLINE_OFFSET); if (nowMs >= deadlineMs) { freeList.remove(i); valuesBuffer.putLongOrdered(counterOffset(counterId), 0L); return counterId; } } return ++idHighWaterMark; }
checkCountersCapacity(counterId); final int recordOffset = metaDataOffset(counterId); checkMetaDataCapacity(recordOffset);
/** * Allocate a new counter with a given label and type. * * @param label to describe the counter. * @param typeId for the type of counter. * @return the id allocated for the counter. */ public int allocate(final String label, final int typeId) { final int counterId = nextCounterId(); checkCountersCapacity(counterId); final int recordOffset = metaDataOffset(counterId); checkMetaDataCapacity(recordOffset); try { metaDataBuffer.putInt(recordOffset + TYPE_ID_OFFSET, typeId); metaDataBuffer.putLong(recordOffset + FREE_FOR_REUSE_DEADLINE_OFFSET, NOT_FREE_TO_REUSE); putLabel(recordOffset, label); metaDataBuffer.putIntOrdered(recordOffset, RECORD_ALLOCATED); } catch (final Exception ex) { freeList.pushInt(counterId); LangUtil.rethrowUnchecked(ex); } return counterId; }
checkCountersCapacity(counterId); final int recordOffset = metaDataOffset(counterId); checkMetaDataCapacity(recordOffset);