use of io.pravega.segmentstore.server.SegmentContainer in project pravega by pravega.
the class SegmentContainerRegistryHealthContributor method doHealthCheck.
@Override
public Status doHealthCheck(Health.HealthBuilder builder) {
for (SegmentContainer container : segmentContainerRegistry.getContainers()) {
this.register(new SegmentContainerHealthContributor(container));
}
Status status = Status.DOWN;
boolean ready = !segmentContainerRegistry.isClosed();
if (ready) {
status = Status.UP;
}
return status;
}
use of io.pravega.segmentstore.server.SegmentContainer in project pravega by pravega.
the class SegmentContainerCollection method invoke.
/**
* Executes the given Function on the StreamSegmentContainer that the given Id maps to.
*
* @param containerId The Id to fetch the Container for.
* @param toInvoke A Function that will be invoked on the Container.
* @param methodName The name of the calling method (for logging purposes).
* @param logArgs (Optional) A vararg array of items to be logged.
* @param <T> Resulting type.
* @return Either the result of toInvoke or a CompletableFuture completed exceptionally with a ContainerNotFoundException
* in case the SegmentContainer that the Id maps to does not exist in this StreamSegmentService.
*/
protected <T> CompletableFuture<T> invoke(int containerId, Function<SegmentContainer, CompletableFuture<T>> toInvoke, String methodName, Object... logArgs) {
long traceId = LoggerHelpers.traceEnter(log, methodName, logArgs);
SegmentContainer container;
try {
container = this.segmentContainerRegistry.getContainer(containerId);
} catch (ContainerNotFoundException ex) {
return Futures.failedFuture(ex);
}
CompletableFuture<T> resultFuture = toInvoke.apply(container);
if (log.isTraceEnabled()) {
resultFuture.thenAccept(r -> LoggerHelpers.traceLeave(log, methodName, traceId, r));
}
return resultFuture;
}
use of io.pravega.segmentstore.server.SegmentContainer in project pravega by pravega.
the class StreamSegmentContainerRegistryTests method testGetContainer.
/**
* Tests the getContainer method for registered and unregistered containers.
*/
@Test
public void testGetContainer() throws Exception {
final int containerCount = 1000;
TestContainerFactory factory = new TestContainerFactory();
@Cleanup StreamSegmentContainerRegistry registry = new StreamSegmentContainerRegistry(factory, executorService());
HashSet<Integer> expectedContainerIds = new HashSet<>();
List<CompletableFuture<ContainerHandle>> handleFutures = new ArrayList<>();
for (int containerId = 0; containerId < containerCount; containerId++) {
handleFutures.add(registry.startContainer(containerId, TIMEOUT));
expectedContainerIds.add(containerId);
}
List<ContainerHandle> handles = Futures.allOfWithResults(handleFutures).join();
HashSet<Integer> actualHandleIds = new HashSet<>();
for (ContainerHandle handle : handles) {
actualHandleIds.add(handle.getContainerId());
SegmentContainer container = registry.getContainer(handle.getContainerId());
Assert.assertTrue("Wrong container Java type.", container instanceof TestContainer);
Assert.assertEquals("Unexpected container Id.", handle.getContainerId(), container.getId());
container.close();
}
AssertExtensions.assertContainsSameElements("Unexpected container ids registered.", expectedContainerIds, actualHandleIds);
AssertExtensions.assertThrows("getContainer did not throw when passed an invalid container id.", () -> registry.getContainer(containerCount + 1), ex -> ex instanceof ContainerNotFoundException);
}
use of io.pravega.segmentstore.server.SegmentContainer in project pravega by pravega.
the class StreamSegmentContainerTests method testExtensions.
/**
* Tests the ability to register extensions.
*/
@Test
public void testExtensions() throws Exception {
String segmentName = getSegmentName(123);
ByteArraySegment data = getAppendData(segmentName, 0);
// Configure extension.
val operationProcessed = new CompletableFuture<SegmentOperation>();
AtomicInteger count = new AtomicInteger();
val writerProcessor = new TestWriterProcessor(op -> {
if (op.getStreamSegmentId() != EXPECTED_METADATA_SEGMENT_ID) {
// We need to exclude any appends that come from the MetadataStore as those do not concern us.
count.incrementAndGet();
if (!operationProcessed.isDone()) {
operationProcessed.complete(op);
}
}
});
val extension = new AtomicReference<TestSegmentContainerExtension>();
SegmentContainerFactory.CreateExtensions additionalExtensions = (container, executor) -> {
Assert.assertTrue("Already created", extension.compareAndSet(null, new TestSegmentContainerExtension(Collections.singleton(writerProcessor))));
return Collections.singletonMap(TestSegmentContainerExtension.class, extension.get());
};
@Cleanup val context = new TestContext(DEFAULT_CONFIG, additionalExtensions);
context.container.startAsync().awaitRunning();
// Verify getExtension().
val p = context.container.getExtension(TestSegmentContainerExtension.class);
Assert.assertEquals("Unexpected result from getExtension().", extension.get(), p);
// Verify Writer Segment Processors are properly wired in.
context.container.createStreamSegment(segmentName, getSegmentType(segmentName), null, TIMEOUT).join();
context.container.append(segmentName, data, null, TIMEOUT).join();
val rawOp = operationProcessed.get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
Assert.assertTrue("Unexpected operation type.", rawOp instanceof CachedStreamSegmentAppendOperation);
// Our operation has been transformed into a CachedStreamSegmentAppendOperation, which means it just points to
// a location in the cache. We do not have access to that cache, so we can only verify its metadata.
val appendOp = (CachedStreamSegmentAppendOperation) rawOp;
Assert.assertEquals("Unexpected offset.", 0, appendOp.getStreamSegmentOffset());
Assert.assertEquals("Unexpected data length.", data.getLength(), appendOp.getLength());
Assert.assertNull("Unexpected attribute updates.", appendOp.getAttributeUpdates());
// Verify extension is closed when the SegmentContainer is closed.
context.container.close();
Assert.assertTrue("Extension not closed.", extension.get().closed.get());
}
Aggregations