public static <T> PersistentAtomicReference<T> create(final Path filename, final JavaType javaType, final Supplier<? extends T> initialValue) throws IOException, InterruptedException { return new PersistentAtomicReference<>(filename, javaType, initialValue); }
public byte[] put(final String key, final byte[] value) throws InterruptedException { Preconditions.checkArgument(key.indexOf('/') == -1); PathUtils.validatePath(ZKPaths.makePath(path, key)); final byte[] prev; synchronized (lock) { final Map<String, byte[]> mutable = Maps.newHashMap(entries.get()); prev = mutable.put(key, value); try { entries.set(ImmutableMap.copyOf(mutable)); } catch (IOException e) { throw new RuntimeException(e); } } reactor.signal(); return prev; }
/** * Set the reference to {@code newValue}. * * @param newValue The value to set. * * @throws IOException If an error occurs working with the file on disk. * @throws InterruptedException If the thread is interrupted. */ public void set(T newValue) throws IOException, InterruptedException { try { set0(newValue); } catch (InterruptedIOException | ClosedByInterruptException e) { throw new InterruptedException(e.getMessage()); } }
public TaskHistoryWriter(final String hostname, final ZooKeeperClient client, final Path backingFile) throws IOException, InterruptedException { this.hostname = hostname; this.client = client; this.backingStore = PersistentAtomicReference.create(backingFile, new TypeReference<ConcurrentMap<JobId, Deque<TaskStatusEvent>>>() { }, new Supplier<ConcurrentMap<JobId, Deque<TaskStatusEvent>>>() { @Override public ConcurrentMap<JobId, Deque<TaskStatusEvent>> get() { return Maps.newConcurrentMap(); } }); this.items = backingStore.get(); // Clean out any errant null values. Normally shouldn't have any, but we did have a few // where it happened, and this will make sure we can get out of a bad state if we get into it. final ImmutableSet<JobId> curKeys = ImmutableSet.copyOf(this.items.keySet()); for (final JobId key : curKeys) { if (this.items.get(key) == null) { this.items.remove(key); } } int itemCount = 0; for (final Deque<TaskStatusEvent> deque : items.values()) { itemCount += deque.size(); } this.count = new AtomicInteger(itemCount); }
final Map<String, T> currentSnapshot = snapshot.get(); snapshot.setUnchecked(newSnapshot); fireNodesChanged();
public Map<String, T> getNodes() { return snapshot.get(); }
public static <T> PersistentAtomicReference<T> create(final String filename, final JavaType javaType, final Supplier<? extends T> initialValue) throws IOException, InterruptedException { return create(FileSystems.getDefault().getPath(filename), javaType, initialValue); }
/** * Set the reference to {@code newValue}, and wraps {@link IOException}s in * {@link RuntimeException}s. * * @param newValue The value to set. * * @throws InterruptedException If the thread is interrupted. */ public void setUnchecked(T newValue) throws InterruptedException { try { set(newValue); } catch (IOException e) { throw new RuntimeException(e); } }
public static <T> PersistentAtomicReference<T> create(final Path filename, final TypeReference<T> typeReference, final Supplier<? extends T> initialValue) throws IOException, InterruptedException { return new PersistentAtomicReference<>(filename, Json.type(typeReference), initialValue); }
log.debug("executions: {}", executions.get()); log.debug("supervisors: {}", supervisors); final Map<JobId, Execution> newExecutions = Maps.newHashMap(executions.get()); for (final Entry<JobId, Task> entry : tasks.entrySet()) { final JobId jobId = entry.getKey(); if (!newExecutions.equals(executions.get())) { executions.setUnchecked(ImmutableMap.copyOf(newExecutions)); for (final Entry<JobId, Execution> entry : executions.get().entrySet()) { final JobId jobId = entry.getKey(); final Execution execution = entry.getValue(); final JobId jobId = entry.getKey(); final Supervisor supervisor = entry.getValue(); final Execution execution = executions.get().get(jobId); supervisor.setGoal(execution.getGoal()); for (final Entry<JobId, Execution> entry : executions.get().entrySet()) { final JobId jobId = entry.getKey(); final Execution execution = entry.getValue(); final Map<JobId, Execution> survivors = Maps.filterKeys(executions.get(), not(in(reapedTasks))); executions.setUnchecked(ImmutableMap.copyOf(survivors));
public byte[] get(final Object key) { return entries.get().get(key); }
private ZooKeeperUpdatingPersistentDirectory(final String name, final ZooKeeperClientProvider provider, final Path stateFile, final String path) throws IOException, InterruptedException { this.provider = provider; this.path = path; this.entries = PersistentAtomicReference.create(stateFile, ENTRIES_TYPE, Suppliers.ofInstance(EMPTY_ENTRIES)); this.reactor = new DefaultReactor(name, new Update(), RETRY_INTERVAL_MILLIS); }
public QueueingHistoryWriter(final ZooKeeperClient client, final Path backingFile) throws IOException, InterruptedException { this.client = checkNotNull(client, "client"); this.backingStore = PersistentAtomicReference.create( checkNotNull(backingFile, "backingFile"), new TypeReference<ConcurrentMap<String, Deque<TEvent>>>(){}, new Supplier<ConcurrentMap<String, Deque<TEvent>>>() { @Override public ConcurrentMap<String, Deque<TEvent>> get() { return Maps.newConcurrentMap(); } }); this.events = backingStore.get(); // Clean out any errant null values. Normally shouldn't have any, but we did have a few // where it happened, and this will make sure we can get out of a bad state if we get into it. final ImmutableSet<String> curKeys = ImmutableSet.copyOf(this.events.keySet()); for (final String key : curKeys) { if (this.events.get(key) == null) { this.events.remove(key); } } int eventCount = 0; for (Deque<TEvent> deque : events.values()) { eventCount += deque.size(); } this.count = new AtomicInteger(eventCount); }
private void add(TaskStatusEvent item) throws InterruptedException { // If too many "globally", toss them while (count.get() >= MAX_TOTAL_SIZE) { getNext(); } final JobId key = item.getStatus().getJob().getId(); final Deque<TaskStatusEvent> deque = getDeque(key); synchronized (deque) { // if too many in the particular deque, toss them while (deque.size() >= MAX_QUEUE_SIZE) { deque.remove(); count.decrementAndGet(); } deque.add(item); count.incrementAndGet(); } try { backingStore.set(items); } catch (ClosedByInterruptException e) { log.debug("Writing task status event to backing store was interrupted"); } catch (IOException e) { // We are best effort after all... log.warn("Failed to write task status event to backing store", e); } }
public static <T> PersistentAtomicReference<T> create(final Path filename, final TypeReference<T> typeReference, final Supplier<? extends T> initialValue) throws IOException, InterruptedException { return new PersistentAtomicReference<>(filename, Json.type(typeReference), initialValue); }
private byte[] remove(final String key) throws InterruptedException { Preconditions.checkArgument(key.indexOf('/') == -1); PathUtils.validatePath(ZKPaths.makePath(path, key)); final byte[] value; synchronized (lock) { final Map<String, byte[]> mutable = Maps.newHashMap(entries.get()); value = mutable.remove(key); try { entries.set(ImmutableMap.copyOf(mutable)); } catch (IOException e) { throw new RuntimeException(e); } } reactor.signal(); return value; }
final Map<String, T> currentSnapshot = snapshot.get(); snapshot.setUnchecked(newSnapshot); fireNodesChanged();
public Set<Map.Entry<String, byte[]>> entrySet() { return entries.get().entrySet(); }
public PersistentPathChildrenCache(final CuratorFramework curator, final String path, final String clusterId, final Path snapshotFile, final JavaType valueType) throws IOException, InterruptedException { this.curator = curator; this.path = path; this.clusterId = clusterId; this.valueType = valueType; final MapType mapType = Json.typeFactory().constructMapType(HashMap.class, Json.type(String.class), valueType); final Supplier<Map<String, T>> empty = Suppliers.ofInstance(Collections.<String, T>emptyMap()); this.snapshot = PersistentAtomicReference.create(snapshotFile, mapType, empty); this.reactor = new DefaultReactor("zk-ppcc:" + path, new Update(), REFRESH_INTERVAL_MILLIS); curator.getConnectionStateListenable().addListener(new ConnectionListener()); }
public static <T> PersistentAtomicReference<T> create(final Path filename, final JavaType javaType, final Supplier<? extends T> initialValue) throws IOException, InterruptedException { return new PersistentAtomicReference<>(filename, javaType, initialValue); }