/** * Applies a journal entry commit to the state machine. * * This method is automatically discovered by the Copycat framework. * * @param commit the commit */ public void applyJournalEntryCommand(Commit<JournalEntryCommand> commit) { JournalEntry entry; try { entry = JournalEntry.parseFrom(commit.command().getSerializedJournalEntry()); } catch (Throwable t) { ProcessUtils.fatalError(LOG, t, "Encountered invalid journal entry in commit: {}.", commit); throw new IllegalStateException(); } applyEntry(entry); }
/** * Applies a journal entry commit to the state machine. * * This method is automatically discovered by the Copycat framework. * * @param commit the commit */ public synchronized void applyJournalEntryCommand(Commit<JournalEntryCommand> commit) { JournalEntry entry; try { entry = JournalEntry.parseFrom(commit.command().getSerializedJournalEntry()); } catch (Exception e) { ProcessUtils.fatalError(LOG, e, "Encountered invalid journal entry in commit: {}.", commit); System.exit(-1); throw new IllegalStateException(e); // We should never reach here. } try { applyEntry(entry); } finally { Preconditions.checkState(commit.index() > mLastAppliedCommitIndex); mLastAppliedCommitIndex = commit.index(); commit.close(); } }
/** * Unregisters an event listener. */ private void unregister(Commit<ResourceCommand.Unregister> commit) { Set<ServerSession> sessions = eventListeners.computeIfAbsent(commit.command().event(), k -> new HashSet<>()); sessions.remove(commit.session()); if (sessions.isEmpty()) { eventListeners.remove(commit.command().event()); } }
/** * Registers an event listener. */ private void register(Commit<ResourceCommand.Register> commit) { Set<ServerSession> sessions = eventListeners.computeIfAbsent(commit.command().event(), k -> new HashSet<>()); sessions.add(commit.session()); }
public void lock(Commit<LockCommand> command) { LockCommand lockCommand = command.command(); try { locks.putIfAbsent(lockCommand.getName(), new ReentrantLock()); locks.get(lockCommand.getName()).lock(); } finally { command.close(); } }
public Integer size(Commit<GetSizeCommand> command) { try { GetSizeCommand sizeCommand = command.command(); if(namedMaps.containsKey(sizeCommand.getMapName())) { int size = namedMaps.get(sizeCommand.getMapName()).size(); LOG.info("Returning map size: {}", size); return size; } else { LOG.info("Returning 0"); return 0; } } finally { command.close(); } } }
/** * Registers a key change listener. */ public void listen(Commit<MapCommands.KeyListen> commit) { Map<Integer, Map<Long, Commit<MapCommands.KeyListen>>> listeners = this.listeners.computeIfAbsent(commit.command().key(), k -> new HashMap<>()); Map<Long, Commit<MapCommands.KeyListen>> sessions = listeners.computeIfAbsent(commit.command().event(), e -> new HashMap<>()); if (!sessions.containsKey(commit.session().id())) { sessions.put(commit.session().id(), commit); } else { commit.release(); } }
/** * Unregisters a key change listener. */ public void unlisten(Commit<MapCommands.KeyUnlisten> commit) { try { Map<Integer, Map<Long, Commit<MapCommands.KeyListen>>> listeners = this.listeners.get(commit.command().key()); if (listeners != null) { Map<Long, Commit<MapCommands.KeyListen>> sessions = listeners.get(commit.command().event()); if (sessions != null) { Commit<MapCommands.KeyListen> listen = sessions.remove(commit.session().id()); if (listen != null) { listen.release(); if (sessions.isEmpty()) { listeners.remove(commit.command().event()); if (listeners.isEmpty()) { this.listeners.remove(commit.command().key()); } } } } } } finally { commit.release(); } }
public void unlock(Commit<UnlockCommand> command) { UnlockCommand unlockCommand = command.command(); try { locks.computeIfPresent(unlockCommand.getName(), (s, reentrantLock) -> { reentrantLock.unlock(); return null; }); } finally { command.close(); } } }
/** * Handles an add commit. */ public boolean add(Commit<QueueCommands.Add> commit) { try { queue.add(commit); notify(new ValueEvent<>(Events.ADD, commit.command().value())); } catch (Exception e) { commit.release(); throw e; } return true; }
/** * Handles an offer commit. */ public boolean offer(Commit<QueueCommands.Offer> commit) { try { if (queue.offer(commit)) { notify(new ValueEvent<>(Events.ADD, commit.command().value())); return true; } else { commit.release(); return false; } } catch (Exception e) { commit.release(); throw e; } }
/** * Handles a put if absent commit. */ public Object putIfAbsent(Commit<MapCommands.PutIfAbsent> commit) { try { final Object key = commit.command().key(); final long ttl = commit.command().ttl(); final Value value = map.get(key); if (value == null) { final Scheduled timer = ttl > 0 ? executor.schedule(Duration.ofMillis(ttl), () -> { Value removed = map.remove(key); if (removed != null) { notify(new EntryEvent<>(Events.REMOVE, new MapEntry<>(key, removed.commit.command().value()))); removed.commit.close(); } }) : null; map.put(key, new Value(commit, timer)); notify(new EntryEvent<>(Events.ADD, new MapEntry<>(key, commit.command().value()))); return null; } else { commit.close(); return value.commit.command().value(); } } catch (Exception e) { commit.close(); throw e; } }
/** * Handles a poll commit. */ public Object poll(Commit<QueueCommands.Poll> commit) { try { Commit<? extends QueueCommands.ValueCommand> value = queue.poll(); if (value != null) { try { notify(new ValueEvent<>(Events.REMOVE, value.command().value())); return value.operation().value(); } finally { value.release(); } } return null; } finally { commit.release(); } }
/** * Handles a replace commit. */ public Object replace(Commit<MapCommands.Replace> commit) { Value value = map.get(commit.operation().key()); if (value != null) { try { final Object key = commit.command().key(); final long ttl = commit.command().ttl(); if (value.timer != null) value.timer.cancel(); final Scheduled timer = ttl > 0 ? executor.schedule(Duration.ofMillis(ttl), () -> { Value removed = map.remove(key); if (removed != null) { notify(new EntryEvent<>(Events.REMOVE, new MapEntry<>(key, removed.commit.command().value()))); removed.commit.close(); } }) : null; map.put(key, new Value(commit, timer)); notify(new EntryEvent<>(Events.UPDATE, new MapEntry<>(key, commit.command().value()))); return value.commit.operation().value(); } finally { value.commit.close(); } } else { commit.close(); } return null; }
if (value.operation().value().equals(commit.operation().value())) { iterator.remove(); notify(new ValueEvent<>(Events.REMOVE, value.command().value())); value.release(); return true; if (value != null) { try { notify(new ValueEvent<>(Events.REMOVE, value.command().value())); return value.operation().value(); } finally {
final Object key = commit.command().key(); value.timer.cancel(); final long ttl = commit.command().ttl(); final Scheduled timer = ttl > 0 ? executor.schedule(Duration.ofMillis(ttl), () -> { Value removed = map.remove(key); if (removed != null) { notify(new EntryEvent<>(Events.REMOVE, new MapEntry<>(key, removed.commit.command().value()))); removed.commit.close();