use of io.pravega.segmentstore.storage.DataLogInitializationException 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.DataLogInitializationException in project pravega by pravega.
the class BookKeeperLog method loadMetadata.
// endregion
// region Metadata Management
/**
* Loads the metadata for the current log, as stored in ZooKeeper.
*
* @return A new LogMetadata object with the desired information, or null if no such node exists.
* @throws DataLogInitializationException If an Exception (other than NoNodeException) occurred.
*/
@VisibleForTesting
LogMetadata loadMetadata() throws DataLogInitializationException {
try {
Stat storingStatIn = new Stat();
byte[] serializedMetadata = this.zkClient.getData().storingStatIn(storingStatIn).forPath(this.logNodePath);
LogMetadata result = LogMetadata.SERIALIZER.deserialize(serializedMetadata);
result.withUpdateVersion(storingStatIn.getVersion());
return result;
} catch (KeeperException.NoNodeException nne) {
// Node does not exist: this is the first time we are accessing this log.
log.warn("{}: No ZNode found for path '{}{}'. This is OK if this is the first time accessing this log.", this.traceObjectId, this.zkClient.getNamespace(), this.logNodePath);
return null;
} catch (Exception ex) {
throw new DataLogInitializationException(String.format("Unable to load ZNode contents for path '%s%s'.", this.zkClient.getNamespace(), this.logNodePath), ex);
}
}
Aggregations