All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.uber.hoodie.common.util.ConsistencyGuard Maven / Gradle / Ivy

The newest version!
/*
 *  Copyright (c) 2019 Uber Technologies, Inc. ([email protected])
 *
 *  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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 */

package com.uber.hoodie.common.util;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.apache.hadoop.fs.Path;

/**
 * Ensures file create/delete operation is visible
 */
public interface ConsistencyGuard {

  /**
   * File Visibility
   */
  enum FileVisibility {
    APPEAR,
    DISAPPEAR,
  }

  /**
   * Wait for file to be listable based on configurable timeout
   * @param filePath
   * @throws IOException when having trouble listing the path
   * @throws TimeoutException when retries exhausted
   */
  void waitTillFileAppears(Path filePath) throws IOException, TimeoutException;

  /**
   * Wait for file to be listable based on configurable timeout
   * @param filePath
   * @throws IOException when having trouble listing the path
   * @throws TimeoutException when retries exhausted
   */
  void waitTillFileDisappears(Path filePath) throws IOException, TimeoutException;

  /**
   * Wait till all passed files belonging to a directory shows up in the listing
   */
  void waitTillAllFilesAppear(String dirPath, List files) throws IOException, TimeoutException;

  /**
   * Wait till all passed files belonging to a directory disappears from listing
   */
  void waitTillAllFilesDisappear(String dirPath, List files) throws IOException, TimeoutException;


  /**
   * Wait Till target visibility is reached
   * @param dirPath  Directory Path
   * @param files    Files
   * @param targetVisibility Target Visibitlity
   * @throws IOException
   * @throws TimeoutException
   */
  default void waitTill(String dirPath, List files, FileVisibility targetVisibility)
      throws IOException, TimeoutException {
    switch (targetVisibility) {
      case APPEAR: {
        waitTillAllFilesAppear(dirPath, files);
        break;
      }
      case DISAPPEAR: {
        waitTillAllFilesDisappear(dirPath, files);
        break;
      }
      default:
        throw new IllegalStateException("Unknown File Visibility");
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy