use of io.pravega.segmentstore.storage.DataLogWriterNotPrimaryException in project pravega by pravega.
the class BookKeeperLog method persistMetadata.
/**
* Persists the given metadata into ZooKeeper.
*
* @param metadata The LogMetadata to persist. At the end of this method, this metadata will have its Version updated
* to the one in ZooKeeper.
* @param create Whether to create (true) or update (false) the data in ZooKeeper.
* @throws DataLogWriterNotPrimaryException If the metadata update failed (if we were asked to create and the node
* already exists or if we had to update and there was a version mismatch).
* @throws DurableDataLogException If another kind of exception occurred.
*/
private void persistMetadata(LogMetadata metadata, boolean create) throws DurableDataLogException {
try {
byte[] serializedMetadata = LogMetadata.SERIALIZER.serialize(metadata).getCopy();
if (create) {
this.zkClient.create().creatingParentsIfNeeded().forPath(this.logNodePath, serializedMetadata);
// Set version to 0 as that will match the ZNode's version.
metadata.withUpdateVersion(0);
} else {
this.zkClient.setData().withVersion(metadata.getUpdateVersion()).forPath(this.logNodePath, serializedMetadata);
// Increment the version to keep up with the ZNode's value (after writing it to ZK).
metadata.withUpdateVersion(metadata.getUpdateVersion() + 1);
}
} catch (KeeperException.NodeExistsException | KeeperException.BadVersionException keeperEx) {
// We were fenced out. Clean up and throw appropriate exception.
throw new DataLogWriterNotPrimaryException(String.format("Unable to acquire exclusive write lock for log (path = '%s%s').", this.zkClient.getNamespace(), this.logNodePath), keeperEx);
} catch (Exception generalEx) {
// General exception. Clean up and rethrow appropriate exception.
throw new DataLogInitializationException(String.format("Unable to update ZNode for path '%s%s'.", this.zkClient.getNamespace(), this.logNodePath), generalEx);
}
log.info("{} Metadata persisted ({}).", this.traceObjectId, metadata);
}
use of io.pravega.segmentstore.storage.DataLogWriterNotPrimaryException in project pravega by pravega.
the class StreamSegmentContainerTests method testWriteFenceOut.
/**
* Tests the behavior of the SegmentContainer when another instance of the same container is activated and fences out
* the first one.
*/
@Test
public void testWriteFenceOut() throws Exception {
final Duration shutdownTimeout = Duration.ofSeconds(5);
@Cleanup TestContext context = new TestContext();
val container1 = context.container;
container1.startAsync().awaitRunning();
val segmentNames = createSegments(context);
@Cleanup val container2 = context.containerFactory.createStreamSegmentContainer(CONTAINER_ID);
container2.startAsync().awaitRunning();
AssertExtensions.assertThrows("Original container did not reject an append operation after being fenced out.", () -> container1.append(segmentNames.get(0), new byte[1], null, TIMEOUT), ex -> ex instanceof DataLogWriterNotPrimaryException);
// Verify we can still write to the second container.
container2.append(segmentNames.get(0), 0, new byte[1], null, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
// Verify container1 is shutting down (give it some time to complete) and that it ends up in a Failed state.
ServiceListeners.awaitShutdown(container1, shutdownTimeout, false);
Assert.assertEquals("Container1 is not in a failed state after fence-out detected.", Service.State.FAILED, container1.state());
Assert.assertTrue("Container1 did not fail with the correct exception.", Exceptions.unwrap(container1.failureCause()) instanceof DataLogWriterNotPrimaryException);
}
use of io.pravega.segmentstore.storage.DataLogWriterNotPrimaryException in project pravega by pravega.
the class DurableLogTests method testAddWithDataLogWriterNotPrimaryException.
/**
* Tests the ability of the DurableLog to handle a DataLogWriterNotPrimaryException.
*/
@Test
public void testAddWithDataLogWriterNotPrimaryException() throws Exception {
int streamSegmentCount = 1;
int appendsPerStreamSegment = 1;
// Setup a DurableLog and start it.
@Cleanup ContainerSetup setup = new ContainerSetup(executorService());
@Cleanup DurableLog durableLog = setup.createDurableLog();
durableLog.startAsync().awaitRunning();
HashSet<Long> streamSegmentIds = createStreamSegmentsInMetadata(streamSegmentCount, setup.metadata);
List<Operation> operations = generateOperations(streamSegmentIds, new HashMap<>(), appendsPerStreamSegment, METADATA_CHECKPOINT_EVERY, false, false);
ErrorInjector<Exception> aSyncErrorInjector = new ErrorInjector<>(count -> true, () -> new CompletionException(new DataLogWriterNotPrimaryException("intentional")));
setup.dataLog.get().setAppendErrorInjectors(null, aSyncErrorInjector);
// Process all generated operations.
List<OperationWithCompletion> completionFutures = processOperations(operations, durableLog);
// Wait for all such operations to complete. We are expecting exceptions, so verify that we do.
AssertExtensions.assertThrows("No operations failed.", OperationWithCompletion.allOf(completionFutures)::join, ex -> ex instanceof IOException || ex instanceof DataLogWriterNotPrimaryException);
// Verify that the OperationProcessor automatically shuts down and that it has the right failure cause.
ServiceListeners.awaitShutdown(durableLog, TIMEOUT, false);
Assert.assertEquals("DurableLog is not in a failed state after fence-out detected.", Service.State.FAILED, durableLog.state());
Assert.assertTrue("DurableLog did not fail with the correct exception.", Exceptions.unwrap(durableLog.failureCause()) instanceof DataLogWriterNotPrimaryException);
}
use of io.pravega.segmentstore.storage.DataLogWriterNotPrimaryException in project pravega by pravega.
the class OperationProcessorTests method testWithDataLogNotPrimaryException.
/**
* Tests the ability of the OperationProcessor handle a DataLogWriterNotPrimaryException.
*/
@Test
public void testWithDataLogNotPrimaryException() throws Exception {
int streamSegmentCount = 1;
int appendsPerStreamSegment = 1;
@Cleanup TestContext context = new TestContext();
// Generate some test data (no need to complicate ourselves with Transactions here; that is tested in the no-failure test).
HashSet<Long> streamSegmentIds = createStreamSegmentsInMetadata(streamSegmentCount, context.metadata);
List<Operation> operations = generateOperations(streamSegmentIds, new HashMap<>(), appendsPerStreamSegment, METADATA_CHECKPOINT_EVERY, false, false);
// Setup an OperationProcessor and start it.
@Cleanup TestDurableDataLog dataLog = TestDurableDataLog.create(CONTAINER_ID, MAX_DATA_LOG_APPEND_SIZE, executorService());
dataLog.initialize(TIMEOUT);
@Cleanup OperationProcessor operationProcessor = new OperationProcessor(context.metadata, context.stateUpdater, dataLog, getNoOpCheckpointPolicy(), executorService());
operationProcessor.startAsync().awaitRunning();
ErrorInjector<Exception> aSyncErrorInjector = new ErrorInjector<>(count -> true, () -> new CompletionException(new DataLogWriterNotPrimaryException("intentional")));
dataLog.setAppendErrorInjectors(null, aSyncErrorInjector);
// Process all generated operations.
List<OperationWithCompletion> completionFutures = processOperations(operations, operationProcessor);
// Wait for all such operations to complete. We are expecting exceptions, so verify that we do.
AssertExtensions.assertThrows("No operations failed.", OperationWithCompletion.allOf(completionFutures)::join, ex -> ex instanceof IOException || ex instanceof DataLogWriterNotPrimaryException);
// Verify that the OperationProcessor automatically shuts down and that it has the right failure cause.
ServiceListeners.awaitShutdown(operationProcessor, TIMEOUT, false);
Assert.assertEquals("OperationProcessor is not in a failed state after fence-out detected.", Service.State.FAILED, operationProcessor.state());
Assert.assertTrue("OperationProcessor did not fail with the correct exception.", operationProcessor.failureCause() instanceof DataLogWriterNotPrimaryException);
}
Aggregations