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