public synchronized boolean transitionToScheduling() { return stageState.compareAndSet(PLANNED, SCHEDULING); }
public synchronized boolean transitionToScheduling() { return stageState.compareAndSet(PLANNED, SCHEDULING); }
@Override public void setNoMorePages() { state.compareAndSet(OPEN, NO_MORE_PAGES); state.compareAndSet(NO_MORE_BUFFERS, FLUSHING); memoryManager.setNoBlockOnFull(); partitions.forEach(ClientBuffer::setNoMorePages); checkFlushComplete(); }
@Override public void setNoMorePages() { state.compareAndSet(OPEN, NO_MORE_PAGES); state.compareAndSet(NO_MORE_BUFFERS, FLUSHING); memoryManager.setNoBlockOnFull(); partitions.forEach(ClientBuffer::setNoMorePages); checkFlushComplete(); }
@Override public void setNoMorePages() { checkState(!Thread.holdsLock(this), "Can not set no more pages while holding a lock on this"); state.compareAndSet(OPEN, NO_MORE_PAGES); state.compareAndSet(NO_MORE_BUFFERS, FLUSHING); memoryManager.setNoBlockOnFull(); safeGetBuffersSnapshot().forEach(ClientBuffer::setNoMorePages); checkFlushComplete(); }
@Override public void setNoMorePages() { checkState(!Thread.holdsLock(this), "Can not set no more pages while holding a lock on this"); state.compareAndSet(OPEN, NO_MORE_PAGES); state.compareAndSet(NO_MORE_BUFFERS, FLUSHING); memoryManager.setNoBlockOnFull(); safeGetBuffersSnapshot().forEach(ClientBuffer::setNoMorePages); checkFlushComplete(); }
public QueryInfo updateQueryInfo(Optional<StageInfo> stageInfo) { QueryInfo queryInfo = getQueryInfo(stageInfo); if (queryInfo.isFinalQueryInfo()) { finalQueryInfo.compareAndSet(Optional.empty(), Optional.of(queryInfo)); } return queryInfo; }
public QueryInfo updateQueryInfo(Optional<StageInfo> stageInfo) { QueryInfo queryInfo = getQueryInfo(stageInfo); if (queryInfo.isFinalQueryInfo()) { finalQueryInfo.compareAndSet(Optional.empty(), Optional.of(queryInfo)); } return queryInfo; }
/** * Sets the state if the current state satisfies the specified predicate. * If the new state does not {@code .equals()} the current state, listeners and waiters will be notified. * * @return true if the state is set */ public boolean setIf(T newState, Predicate<T> predicate) { checkState(!Thread.holdsLock(lock), "Can not set state while holding the lock"); requireNonNull(newState, "newState is null"); while (true) { // check if the current state passes the predicate T currentState = get(); // change to same state is not a change, and does not notify the notify listeners if (currentState.equals(newState)) { return false; } // do not call predicate while holding the lock if (!predicate.test(currentState)) { return false; } // if state did not change while, checking the predicate, apply the new state if (compareAndSet(currentState, newState)) { return true; } } }
public void setAllTasksFinal(Iterable<TaskInfo> finalTaskInfos) { requireNonNull(finalTaskInfos, "finalTaskInfos is null"); checkState(stageState.get().isDone()); StageInfo stageInfo = getStageInfo(() -> finalTaskInfos); checkArgument(stageInfo.isCompleteInfo(), "finalTaskInfos are not all done"); finalStageInfo.compareAndSet(Optional.empty(), Optional.of(stageInfo)); }
public void setAllTasksFinal(Iterable<TaskInfo> finalTaskInfos) { requireNonNull(finalTaskInfos, "finalTaskInfos is null"); checkState(stageState.get().isDone()); StageInfo stageInfo = getStageInfo(() -> finalTaskInfos); checkArgument(stageInfo.isCompleteInfo(), "finalTaskInfos are not all done"); finalStageInfo.compareAndSet(Optional.empty(), Optional.of(stageInfo)); }
@Override public void setNoMorePages() { checkState(!Thread.holdsLock(this), "Can not set no more pages while holding a lock on this"); state.compareAndSet(OPEN, NO_MORE_PAGES); state.compareAndSet(NO_MORE_BUFFERS, FLUSHING); memoryManager.setNoBlockOnFull(); masterBuffer.setNoMorePages(); // process any pending reads from the client buffers for (ClientBuffer clientBuffer : safeGetBuffersSnapshot()) { clientBuffer.loadPagesIfNecessary(masterBuffer); } checkFlushComplete(); }
@Override public void setNoMorePages() { checkState(!Thread.holdsLock(this), "Can not set no more pages while holding a lock on this"); state.compareAndSet(OPEN, NO_MORE_PAGES); state.compareAndSet(NO_MORE_BUFFERS, FLUSHING); memoryManager.setNoBlockOnFull(); masterBuffer.setNoMorePages(); // process any pending reads from the client buffers for (ClientBuffer clientBuffer : safeGetBuffersSnapshot()) { clientBuffer.loadPagesIfNecessary(masterBuffer); } checkFlushComplete(); }
public PartitionedOutputBuffer( String taskInstanceId, StateMachine<BufferState> state, OutputBuffers outputBuffers, DataSize maxBufferSize, Supplier<LocalMemoryContext> systemMemoryContextSupplier, Executor notificationExecutor) { this.state = requireNonNull(state, "state is null"); requireNonNull(outputBuffers, "outputBuffers is null"); checkArgument(outputBuffers.getType() == PARTITIONED, "Expected a PARTITIONED output buffer descriptor"); checkArgument(outputBuffers.isNoMoreBufferIds(), "Expected a final output buffer descriptor"); this.outputBuffers = outputBuffers; this.memoryManager = new OutputBufferMemoryManager( requireNonNull(maxBufferSize, "maxBufferSize is null").toBytes(), requireNonNull(systemMemoryContextSupplier, "systemMemoryContextSupplier is null"), requireNonNull(notificationExecutor, "notificationExecutor is null")); ImmutableList.Builder<ClientBuffer> partitions = ImmutableList.builder(); for (OutputBufferId bufferId : outputBuffers.getBuffers().keySet()) { ClientBuffer partition = new ClientBuffer(taskInstanceId, bufferId); partitions.add(partition); } this.partitions = partitions.build(); state.compareAndSet(OPEN, NO_MORE_BUFFERS); state.compareAndSet(NO_MORE_PAGES, FLUSHING); checkFlushComplete(); }
public PartitionedOutputBuffer( String taskInstanceId, StateMachine<BufferState> state, OutputBuffers outputBuffers, DataSize maxBufferSize, Supplier<LocalMemoryContext> systemMemoryContextSupplier, Executor notificationExecutor) { this.state = requireNonNull(state, "state is null"); requireNonNull(outputBuffers, "outputBuffers is null"); checkArgument(outputBuffers.getType() == PARTITIONED, "Expected a PARTITIONED output buffer descriptor"); checkArgument(outputBuffers.isNoMoreBufferIds(), "Expected a final output buffer descriptor"); this.outputBuffers = outputBuffers; this.memoryManager = new OutputBufferMemoryManager( requireNonNull(maxBufferSize, "maxBufferSize is null").toBytes(), requireNonNull(systemMemoryContextSupplier, "systemMemoryContextSupplier is null"), requireNonNull(notificationExecutor, "notificationExecutor is null")); ImmutableList.Builder<ClientBuffer> partitions = ImmutableList.builder(); for (OutputBufferId bufferId : outputBuffers.getBuffers().keySet()) { ClientBuffer partition = new ClientBuffer(taskInstanceId, bufferId); partitions.add(partition); } this.partitions = partitions.build(); state.compareAndSet(OPEN, NO_MORE_BUFFERS); state.compareAndSet(NO_MORE_PAGES, FLUSHING); checkFlushComplete(); }
@Test public void testCompareAndSet() throws Exception { StateMachine<State> stateMachine = new StateMachine<>("test", executor, State.BREAKFAST, ImmutableSet.of(State.DINNER)); assertEquals(stateMachine.get(), State.BREAKFAST); // no match with new state assertNoStateChange(stateMachine, () -> stateMachine.compareAndSet(State.DINNER, State.LUNCH)); // match with new state assertStateChange(stateMachine, () -> stateMachine.compareAndSet(State.BREAKFAST, State.LUNCH), State.LUNCH); // no match with same state assertNoStateChange(stateMachine, () -> stateMachine.compareAndSet(State.BREAKFAST, State.LUNCH)); // match with same state assertNoStateChange(stateMachine, () -> stateMachine.compareAndSet(State.LUNCH, State.LUNCH)); // transition to a final state assertStateChange(stateMachine, () -> stateMachine.compareAndSet(State.LUNCH, State.DINNER), State.DINNER); // attempt transition from a final state assertNoStateChange(stateMachine, () -> { try { stateMachine.compareAndSet(State.DINNER, State.LUNCH); fail("expected IllegalStateException"); } catch (IllegalStateException expected) { } }); assertNoStateChange(stateMachine, () -> stateMachine.compareAndSet(State.DINNER, State.DINNER)); }
@Test public void testCompareAndSet() throws Exception { StateMachine<State> stateMachine = new StateMachine<>("test", executor, State.BREAKFAST, ImmutableSet.of(State.DINNER)); assertEquals(stateMachine.get(), State.BREAKFAST); // no match with new state assertNoStateChange(stateMachine, () -> stateMachine.compareAndSet(State.DINNER, State.LUNCH)); // match with new state assertStateChange(stateMachine, () -> stateMachine.compareAndSet(State.BREAKFAST, State.LUNCH), State.LUNCH); // no match with same state assertNoStateChange(stateMachine, () -> stateMachine.compareAndSet(State.BREAKFAST, State.LUNCH)); // match with same state assertNoStateChange(stateMachine, () -> stateMachine.compareAndSet(State.LUNCH, State.LUNCH)); // transition to a final state assertStateChange(stateMachine, () -> stateMachine.compareAndSet(State.LUNCH, State.DINNER), State.DINNER); // attempt transition from a final state assertNoStateChange(stateMachine, () -> { try { stateMachine.compareAndSet(State.DINNER, State.LUNCH); fail("expected IllegalStateException"); } catch (IllegalStateException expected) { } }); assertNoStateChange(stateMachine, () -> stateMachine.compareAndSet(State.DINNER, State.DINNER)); }
@Override public void setOutputBuffers(OutputBuffers newOutputBuffers) { checkState(!Thread.holdsLock(this), "Can not set output buffers while holding a lock on this"); requireNonNull(newOutputBuffers, "newOutputBuffers is null"); synchronized (this) { // ignore buffers added after query finishes, which can happen when a query is canceled // also ignore old versions, which is normal BufferState state = this.state.get(); if (state.isTerminal() || outputBuffers.getVersion() >= newOutputBuffers.getVersion()) { return; } // verify this is valid state change outputBuffers.checkValidTransition(newOutputBuffers); outputBuffers = newOutputBuffers; // add the new buffers for (OutputBufferId outputBufferId : outputBuffers.getBuffers().keySet()) { getBuffer(outputBufferId); } // update state if no more buffers is set if (outputBuffers.isNoMoreBufferIds()) { this.state.compareAndSet(OPEN, NO_MORE_BUFFERS); this.state.compareAndSet(NO_MORE_PAGES, FLUSHING); } } if (!state.get().canAddBuffers()) { noMoreBuffers(); } checkFlushComplete(); }
synchronized void updateTaskInfo(TaskInfo newValue) { boolean updated = taskInfo.setIf(newValue, oldValue -> { TaskStatus oldTaskStatus = oldValue.getTaskStatus(); TaskStatus newTaskStatus = newValue.getTaskStatus(); if (oldTaskStatus.getState().isDone()) { // never update if the task has reached a terminal state return false; } // don't update to an older version (same version is ok) return newTaskStatus.getVersion() >= oldTaskStatus.getVersion(); }); if (updated && newValue.getTaskStatus().getState().isDone()) { finalTaskInfo.compareAndSet(Optional.empty(), Optional.of(newValue)); stop(); } }
synchronized void updateTaskInfo(TaskInfo newValue) { boolean updated = taskInfo.setIf(newValue, oldValue -> { TaskStatus oldTaskStatus = oldValue.getTaskStatus(); TaskStatus newTaskStatus = newValue.getTaskStatus(); if (oldTaskStatus.getState().isDone()) { // never update if the task has reached a terminal state return false; } // don't update to an older version (same version is ok) return newTaskStatus.getVersion() >= oldTaskStatus.getVersion(); }); if (updated && newValue.getTaskStatus().getState().isDone()) { finalTaskInfo.compareAndSet(Optional.empty(), Optional.of(newValue)); stop(); } }