protected String get(Commit<String> commit) { return map.get(commit.value()); }
protected long index(Commit<Void> commit) { return commit.index(); }
@Override public byte[] apply(Commit<byte[]> commit) { log.trace("Executing {}", commit); this.operationType = commit.operation().type(); this.timestamp = commit.wallClockTime().unixTimestamp(); // Look up the registered callback for the operation. Function<Commit<byte[]>, byte[]> operation = operations.get(commit.operation().id()); if (operation == null) { throw new IllegalStateException("Unknown state machine operation: " + commit.operation()); } else { // Execute the operation. If the operation return value is a Future, await the result, // otherwise immediately complete the execution future. try { return operation.apply(commit); } catch (Exception e) { log.warn("State machine operation failed: {}", e.getMessage()); throw new PrimitiveException.ServiceException(e); } finally { runTasks(); } } }
@Override public <T> void register(OperationId operationId, Consumer<Commit<T>> callback) { checkNotNull(operationId, "operationId cannot be null"); checkNotNull(callback, "callback cannot be null"); handle(operationId, commit -> { callback.accept(commit.map(this::decode)); return null; }); }
protected String get(Commit<String> commit) { return map.get(commit.value()); }
protected long index(Commit<Void> commit) { return commit.index(); } }
@Override public <T, R> void register(OperationId operationId, Function<Commit<T>, R> callback) { checkNotNull(operationId, "operationId cannot be null"); checkNotNull(callback, "callback cannot be null"); handle(operationId, commit -> encode(callback.apply(commit.map(this::decode)))); }
protected long index(Commit<Void> commit) { return commit.index(); } }
/** * Applies an {@link PrimaryElectorOperations.GetTerm} commit. * * @param commit GetLeadership commit * @return leader */ protected PrimaryTerm getTerm(Commit<? extends PrimaryElectorOperations.GetTerm> commit) { PartitionId partitionId = commit.value().partitionId(); try { return term(partitionId); } catch (Exception e) { getLogger().error("State machine operation failed", e); throw Throwables.propagate(e); } }
executor.register(operationId, args -> { try { method.invoke(this, (Object[]) args.value()); } catch (IllegalAccessException | InvocationTargetException e) { throw new PrimitiveException.ServiceException(e); executor.register(operationId, args -> { try { return method.invoke(this, (Object[]) args.value()); } catch (IllegalAccessException | InvocationTargetException e) { throw new PrimitiveException.ServiceException(e);