public byte[] toBytes() { if (idBytes == null) { idBytes = Bytes.toBytes(toString()); } return idBytes; } }
/** * Updates stream properties in the property store. */ private ListenableFuture<CoordinatorStreamProperties> updateProperties(StreamId streamId, final CoordinatorStreamProperties properties) { return propertyStore.update(streamId.toString(), new SyncPropertyUpdater<CoordinatorStreamProperties>() { @Override protected CoordinatorStreamProperties compute(@Nullable CoordinatorStreamProperties oldProperties) { if (oldProperties == null) { return properties; } // Merge the old and new properties. return new CoordinatorStreamProperties( firstNotNull(properties.getTTL(), oldProperties.getTTL()), firstNotNull(properties.getFormat(), oldProperties.getFormat()), firstNotNull(properties.getNotificationThresholdMB(), oldProperties.getNotificationThresholdMB()), firstNotNull(properties.getGeneration(), oldProperties.getGeneration()), firstNotNull(properties.getDescription(), oldProperties.getDescription()), firstNotNull(properties.getOwnerPrincipal(), oldProperties.getOwnerPrincipal())); } }); }
@Override public void deleteStream(StreamId streamId, Runnable action) throws Exception { Lock lock = getLock(streamId); lock.lock(); try { action.run(); // TODO: CDAP-2161 Ideally would be deleting the property. However it is not supported by PropertyStore right now. propertyStore.set(streamId.toString(), null).get(); streamDeleted(streamId); } finally { lock.unlock(); } }
@Override protected Lock getLock(StreamId streamId) { // It's ok to create new locks every time as it's backed by ZK for distributed lock ZKClient lockZKClient = ZKClients.namespace(zkClient, "/" + Constants.Service.STREAMS + "/locks"); return new ReentrantDistributedLock(lockZKClient, streamId.toString()); }
@Override public Cancellable addListener(StreamId streamId, StreamPropertyListener listener) { return propertyStore.addChangeListener(streamId.toString(), new StreamPropertyChangeListener(listener)); }
@Nullable @Override public ResourceRequirement apply(@Nullable ResourceRequirement existingRequirement) { try { // Create one requirement for the resource coordinator for all the streams. // One stream is identified by one partition ResourceRequirement.Builder builder = ResourceRequirement.builder(Constants.Service.STREAMS); for (Map.Entry<NamespaceId, StreamSpecification> streamSpecEntry : streamMetaStore.listStreams().entries()) { StreamId streamId = streamSpecEntry.getKey().stream(streamSpecEntry.getValue().getName()); LOG.debug("Adding {} stream as a resource to the coordinator to manager streams leaders.", streamId); builder.addPartition(new ResourceRequirement.Partition(streamId.toString(), 1)); } return builder.build(); } catch (Throwable e) { LOG.warn("Could not create requirement for coordinator in Stream handler leader: " + e.getMessage()); LOG.debug("Could not create requirement for coordinator in Stream handler leader", e); throw Throwables.propagate(e); } } };
@Nullable @Override public ResourceRequirement apply(@Nullable ResourceRequirement existingRequirement) { LOG.debug("Modifying requirement to remove stream {}", streamId); if (existingRequirement == null) { return null; } Set<ResourceRequirement.Partition> partitions = existingRequirement.getPartitions(); ResourceRequirement.Builder builder = ResourceRequirement.builder(Constants.Service.STREAMS); for (ResourceRequirement.Partition partition : partitions) { if (!partition.getName().equals(streamId.toString())) { builder.addPartition(partition); } } return builder.build(); } });
@Nullable @Override public ResourceRequirement apply(@Nullable ResourceRequirement existingRequirement) { LOG.debug("Modifying requirement to add stream {} as a resource", streamId); Set<ResourceRequirement.Partition> partitions; if (existingRequirement != null) { partitions = existingRequirement.getPartitions(); } else { partitions = ImmutableSet.of(); } ResourceRequirement.Partition newPartition = new ResourceRequirement.Partition(streamId.toString(), 1); if (partitions.contains(newPartition)) { return null; } ResourceRequirement.Builder builder = ResourceRequirement.builder(Constants.Service.STREAMS); builder.addPartition(newPartition); for (ResourceRequirement.Partition partition : partitions) { builder.addPartition(partition); } return builder.build(); } });