use of org.apache.pulsar.metadata.api.Notification in project incubator-pulsar by apache.
the class EtcdMetadataStore method handleWatchResponse.
private void handleWatchResponse(WatchResponse watchResponse) {
watchResponse.getEvents().forEach(we -> {
String path = we.getKeyValue().getKey().toString(StandardCharsets.UTF_8);
if (we.getEventType() == WatchEvent.EventType.PUT) {
if (we.getKeyValue().getVersion() == 1) {
receivedNotification(new Notification(NotificationType.Created, path));
notifyParentChildrenChanged(path);
} else {
receivedNotification(new Notification(NotificationType.Modified, path));
}
} else if (we.getEventType() == WatchEvent.EventType.DELETE) {
receivedNotification(new Notification(NotificationType.Deleted, path));
notifyParentChildrenChanged(path);
}
});
}
use of org.apache.pulsar.metadata.api.Notification in project pulsar by yahoo.
the class RocksdbMetadataStore method storePut.
@Override
protected CompletableFuture<Stat> storePut(String path, byte[] data, Optional<Long> expectedVersion, EnumSet<CreateOption> options) {
if (log.isDebugEnabled()) {
log.debug("storePut.path={},instanceId={}", path, instanceId);
}
try {
dbStateLock.readLock().lock();
if (state == State.CLOSED) {
throw new MetadataStoreException.AlreadyClosedException("");
}
try (Transaction transaction = db.beginTransaction(optionSync)) {
byte[] pathBytes = toBytes(path);
byte[] oldValueData = transaction.getForUpdate(optionDontCache, pathBytes, false);
MetaValue metaValue = MetaValue.parse(oldValueData);
if (expectedVersion.isPresent()) {
if (metaValue == null && expectedVersion.get() != -1 || metaValue != null && !expectedVersion.get().equals(metaValue.getVersion())) {
throw new MetadataStoreException.BadVersionException(String.format("Version mismatch, actual=%s, expect=%s", metaValue == null ? null : metaValue.getVersion(), expectedVersion.get()));
}
}
boolean created = false;
long timestamp = System.currentTimeMillis();
if (metaValue == null) {
// create new node
metaValue = new MetaValue();
metaValue.version = 0;
metaValue.createdTimestamp = timestamp;
metaValue.ephemeral = options.contains(CreateOption.Ephemeral);
if (options.contains(CreateOption.Sequential)) {
path += sequentialIdGenerator.getAndIncrement();
pathBytes = toBytes(path);
transaction.put(SEQUENTIAL_ID_KEY, toBytes(sequentialIdGenerator.get()));
}
created = true;
} else {
// update old node
metaValue.version++;
}
metaValue.modifiedTimestamp = timestamp;
metaValue.owner = instanceId;
metaValue.data = data;
// handle Sequential
transaction.put(pathBytes, metaValue.serialize());
transaction.commit();
receivedNotification(new Notification(created ? NotificationType.Created : NotificationType.Modified, path));
if (created) {
notifyParentChildrenChanged(path);
}
return CompletableFuture.completedFuture(new Stat(path, metaValue.version, metaValue.createdTimestamp, metaValue.modifiedTimestamp, metaValue.ephemeral, true));
}
} catch (Throwable e) {
return FutureUtil.failedFuture(MetadataStoreException.wrap(e));
} finally {
dbStateLock.readLock().unlock();
}
}
use of org.apache.pulsar.metadata.api.Notification in project pulsar by yahoo.
the class AbstractMetadataStore method notifyParentChildrenChanged.
protected void notifyParentChildrenChanged(String path) {
String parent = parent(path);
while (parent != null) {
receivedNotification(new Notification(NotificationType.ChildrenChanged, parent));
parent = parent(parent);
}
}
use of org.apache.pulsar.metadata.api.Notification in project pulsar by apache.
the class RocksdbMetadataStore method storePut.
@Override
protected CompletableFuture<Stat> storePut(String path, byte[] data, Optional<Long> expectedVersion, EnumSet<CreateOption> options) {
if (log.isDebugEnabled()) {
log.debug("storePut.path={},instanceId={}", path, instanceId);
}
try {
dbStateLock.readLock().lock();
if (state == State.CLOSED) {
throw new MetadataStoreException.AlreadyClosedException("");
}
try (Transaction transaction = db.beginTransaction(optionSync)) {
byte[] pathBytes = toBytes(path);
byte[] oldValueData = transaction.getForUpdate(optionDontCache, pathBytes, false);
MetaValue metaValue = MetaValue.parse(oldValueData);
if (expectedVersion.isPresent()) {
if (metaValue == null && expectedVersion.get() != -1 || metaValue != null && !expectedVersion.get().equals(metaValue.getVersion())) {
throw new MetadataStoreException.BadVersionException(String.format("Version mismatch, actual=%s, expect=%s", metaValue == null ? null : metaValue.getVersion(), expectedVersion.get()));
}
}
boolean created = false;
long timestamp = System.currentTimeMillis();
if (metaValue == null) {
// create new node
metaValue = new MetaValue();
metaValue.version = 0;
metaValue.createdTimestamp = timestamp;
metaValue.ephemeral = options.contains(CreateOption.Ephemeral);
if (options.contains(CreateOption.Sequential)) {
path += sequentialIdGenerator.getAndIncrement();
pathBytes = toBytes(path);
transaction.put(SEQUENTIAL_ID_KEY, toBytes(sequentialIdGenerator.get()));
}
created = true;
} else {
// update old node
metaValue.version++;
}
metaValue.modifiedTimestamp = timestamp;
metaValue.owner = instanceId;
metaValue.data = data;
// handle Sequential
transaction.put(pathBytes, metaValue.serialize());
transaction.commit();
receivedNotification(new Notification(created ? NotificationType.Created : NotificationType.Modified, path));
if (created) {
notifyParentChildrenChanged(path);
}
return CompletableFuture.completedFuture(new Stat(path, metaValue.version, metaValue.createdTimestamp, metaValue.modifiedTimestamp, metaValue.ephemeral, true));
}
} catch (Throwable e) {
return FutureUtil.failedFuture(MetadataStoreException.wrap(e));
} finally {
dbStateLock.readLock().unlock();
}
}
use of org.apache.pulsar.metadata.api.Notification in project pulsar by apache.
the class RocksdbMetadataStore method storeDelete.
@Override
protected CompletableFuture<Void> storeDelete(String path, Optional<Long> expectedVersion) {
if (log.isDebugEnabled()) {
log.debug("storeDelete.path={},instanceId={}", path, instanceId);
}
try {
dbStateLock.readLock().lock();
if (state == State.CLOSED) {
throw new MetadataStoreException.AlreadyClosedException("");
}
try (Transaction transaction = db.beginTransaction(optionSync)) {
byte[] pathBytes = toBytes(path);
byte[] oldValueData = transaction.getForUpdate(optionDontCache, pathBytes, false);
MetaValue metaValue = MetaValue.parse(oldValueData);
if (metaValue == null) {
throw new MetadataStoreException.NotFoundException(String.format("path %s not found.", path));
}
if (expectedVersion.isPresent() && !expectedVersion.get().equals(metaValue.getVersion())) {
throw new MetadataStoreException.BadVersionException(String.format("Version mismatch, actual=%s, expect=%s", metaValue.getVersion(), expectedVersion.get()));
}
transaction.delete(pathBytes);
transaction.commit();
receivedNotification(new Notification(NotificationType.Deleted, path));
notifyParentChildrenChanged(path);
return CompletableFuture.completedFuture(null);
}
} catch (Throwable e) {
return FutureUtil.failedFuture(MetadataStoreException.wrap(e));
} finally {
dbStateLock.readLock().unlock();
}
}
Aggregations