gobblin.hive.HiveLock Maven / Gradle / Ivy
/*
 * Copyright (C) 2014-2016 LinkedIn Corp. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.
 */
package gobblin.hive;
import java.util.concurrent.locks.Lock;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.util.concurrent.Striped;
import gobblin.util.AutoCloseableLock;
/**
 * A striped lock class for Hive databases or tables. To get a lock, use {@link #getDbLock}, {@link #getTableLock}
 * or {@link #getPartitionLock}, which returns a {@link AutoCloseableLock} object that is already locked.
 *
 * 
 *   Obtaining a table lock does not lock the database, which permits concurrent operations on different
 *   tables in the same database. Similarly, obtianing a partition lock does not lock the table or the database.
 * 
 */
public class HiveLock {
  private static final Joiner JOINER = Joiner.on(' ').skipNulls();
  private final Striped locks = Striped.lazyWeakLock(Integer.MAX_VALUE);
  public AutoCloseableLock getDbLock(String dbName) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(dbName));
    return new AutoCloseableLock(this.locks.get(dbName));
  }
  public AutoCloseableLock getTableLock(String dbName, String tableName) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(dbName));
    Preconditions.checkArgument(!Strings.isNullOrEmpty(tableName));
    return new AutoCloseableLock(this.locks.get(JOINER.join(dbName, tableName)));
  }
  public AutoCloseableLock getPartitionLock(String dbName, String tableName, Iterable partitionValues) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(dbName));
    Preconditions.checkArgument(!Strings.isNullOrEmpty(tableName));
    Preconditions.checkArgument(partitionValues.iterator().hasNext());
    return new AutoCloseableLock(this.locks.get(JOINER.join(dbName, tableName, JOINER.join(partitionValues))));
  }
}
      © 2015 - 2025 Weber Informatics LLC | Privacy Policy