001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver.wal;
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022/**
023 * A 'truck' to carry a payload across the ring buffer from Handler to WAL. Has EITHER a
024 * {@link FSWALEntry} for making an append OR it has a {@link SyncFuture} to represent a 'sync'
025 * invocation. Truck instances are reused by the disruptor when it gets around to it so their
026 * payload references must be discarded on consumption to release them to GC.
027 */
028@InterfaceAudience.Private
029final class RingBufferTruck {
030
031  public enum Type {
032    APPEND,
033    SYNC,
034    EMPTY
035  }
036
037  private Type type = Type.EMPTY;
038
039  /**
040   * Either this syncFuture is set or entry is set, but not both.
041   */
042  private SyncFuture sync;
043  private FSWALEntry entry;
044
045  /**
046   * Load the truck with a {@link FSWALEntry}.
047   */
048  void load(FSWALEntry entry) {
049    this.entry = entry;
050    this.type = Type.APPEND;
051  }
052
053  /**
054   * Load the truck with a {@link SyncFuture}.
055   */
056  void load(final SyncFuture syncFuture) {
057    this.sync = syncFuture;
058    this.type = Type.SYNC;
059  }
060
061  /** Returns the type of this truck's payload. */
062  Type type() {
063    return type;
064  }
065
066  /**
067   * Unload the truck of its {@link FSWALEntry} payload. The internal reference is released.
068   */
069  FSWALEntry unloadAppend() {
070    FSWALEntry entry = this.entry;
071    this.entry = null;
072    this.type = Type.EMPTY;
073    return entry;
074  }
075
076  /**
077   * Unload the truck of its {@link SyncFuture} payload. The internal reference is released.
078   */
079  SyncFuture unloadSync() {
080    SyncFuture sync = this.sync;
081    this.sync = null;
082    this.type = Type.EMPTY;
083    return sync;
084  }
085}