|
| 1 | +/* |
| 2 | + * Copyright (2021) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.spark.sql.delta.fuzzer |
| 18 | + |
| 19 | +import java.util.concurrent.atomic.AtomicInteger |
| 20 | + |
| 21 | +/** |
| 22 | + * An atomic barrier is similar to a countdown latch, |
| 23 | + * except that the content is a state transition system with semantic meaning |
| 24 | + * instead of a simple counter. |
| 25 | + * |
| 26 | + * It is designed with a single writer ("unblocker") thread and a single reader ("waiter") thread |
| 27 | + * in mind. It is concurrency safe with more writers and readers, but using more is likely to cause |
| 28 | + * race conditions for legal transitions. That is to say, trying to perform an otherwise |
| 29 | + * legal transition twice is illegal and may occur if there is more than one unblocker or |
| 30 | + * waiter thread. |
| 31 | + * Having additional passive state observers that only call [[load()]] is never an issue. |
| 32 | + * |
| 33 | + * Legal transitions are: |
| 34 | + * - BLOCKED -> UNBLOCKED |
| 35 | + * - BLOCKED -> REQUESTED |
| 36 | + * - REQUESTED -> UNBLOCKED |
| 37 | + * - UNBLOCKED -> PASSED |
| 38 | + */ |
| 39 | +class AtomicBarrier { |
| 40 | + |
| 41 | + import AtomicBarrier._ |
| 42 | + |
| 43 | + private final val state: AtomicInteger = new AtomicInteger(State.Blocked.ordinal) |
| 44 | + |
| 45 | + /** Get the current state. */ |
| 46 | + def load(): State = { |
| 47 | + val ordinal = state.get() |
| 48 | + // We should never be putting illegal state ordinals into `state`, |
| 49 | + // so this should always succeed. |
| 50 | + stateIndex(ordinal) |
| 51 | + } |
| 52 | + |
| 53 | + /** Transition to the Unblocked state. */ |
| 54 | + def unblock(): Unit = { |
| 55 | + // Just hot-retry this, since it never needs to wait to make progress. |
| 56 | + var successful = false |
| 57 | + while(!successful) { |
| 58 | + val currentValue = state.get() |
| 59 | + if (currentValue == State.Blocked.ordinal || currentValue == State.Requested.ordinal) { |
| 60 | + this.synchronized { |
| 61 | + successful = state.compareAndSet(currentValue, State.Unblocked.ordinal) |
| 62 | + if (successful) { |
| 63 | + this.notifyAll() |
| 64 | + } |
| 65 | + } |
| 66 | + } else { |
| 67 | + // if it's in any other state we will never make progress |
| 68 | + throw new IllegalStateTransitionException(stateIndex(currentValue), State.Unblocked) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** Wait until this barrier can be passed and then mark it as Passed. */ |
| 74 | + def waitToPass(): Unit = { |
| 75 | + while (true) { |
| 76 | + val currentState = load() |
| 77 | + currentState match { |
| 78 | + case State.Unblocked => |
| 79 | + val updated = state.compareAndSet(currentState.ordinal, State.Passed.ordinal) |
| 80 | + if (updated) { |
| 81 | + return |
| 82 | + } |
| 83 | + case State.Passed => |
| 84 | + throw new IllegalStateTransitionException(State.Passed, State.Passed) |
| 85 | + case State.Requested => |
| 86 | + this.synchronized { |
| 87 | + if (load().ordinal == State.Requested.ordinal) { |
| 88 | + this.wait() |
| 89 | + } |
| 90 | + } |
| 91 | + case State.Blocked => |
| 92 | + this.synchronized { |
| 93 | + val updated = state.compareAndSet(currentState.ordinal, State.Requested.ordinal) |
| 94 | + if (updated) { |
| 95 | + this.wait() |
| 96 | + } |
| 97 | + } // else (if we didn't succeed) just hot-retry until we do |
| 98 | + // (or more likely pass, since unblocking is the only legal concurrent |
| 99 | + // update with a single concurrent "waiter") |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + override def toString: String = s"AtomicBarrier(state=${load()})" |
| 105 | +} |
| 106 | + |
| 107 | +object AtomicBarrier { |
| 108 | + |
| 109 | + sealed trait State { |
| 110 | + def ordinal: Int |
| 111 | + } |
| 112 | + |
| 113 | + object State { |
| 114 | + case object Blocked extends State { |
| 115 | + override final val ordinal = 0 |
| 116 | + } |
| 117 | + case object Unblocked extends State { |
| 118 | + override final val ordinal = 1 |
| 119 | + } |
| 120 | + case object Requested extends State { |
| 121 | + override final val ordinal = 2 |
| 122 | + } |
| 123 | + case object Passed extends State { |
| 124 | + override final val ordinal = 3 |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + final val stateIndex: Map[Int, State] = |
| 129 | + List(State.Blocked, State.Unblocked, State.Requested, State.Passed) |
| 130 | + .map(state => state.ordinal -> state) |
| 131 | + .toMap |
| 132 | +} |
| 133 | + |
| 134 | +class IllegalStateTransitionException(fromState: AtomicBarrier.State, toState: AtomicBarrier.State) |
| 135 | + extends RuntimeException(s"State transition from $fromState to $toState is illegal.") |
0 commit comments