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

com.twitter.distributedlog.util.Allocator Maven / Gradle / Ivy

The newest version!
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.twitter.distributedlog.util;

import com.twitter.distributedlog.io.AsyncCloseable;
import com.twitter.distributedlog.io.AsyncDeleteable;
import com.twitter.distributedlog.util.Transaction.OpListener;
import com.twitter.util.Future;

import java.io.IOException;

/**
 * A common interface to allocate I under transaction T.
 *
 * 

Usage Example

* * Here is an example on demonstrating how `Allocator` works. * *
 {@code
 * Allocator allocator = ...;
 *
 * // issue an allocate request
 * try {
 *   allocator.allocate();
 * } catch (IOException ioe) {
 *   // handle the exception
 *   ...
 *   return;
 * }
 *
 * // Start a transaction
 * final Transaction txn = ...;
 *
 * // Try obtain object I
 * Future tryObtainFuture = allocator.tryObtain(txn, new OpListener() {
 *     public void onCommit(I resource) {
 *         // the obtain succeed, process with the resource
 *     }
 *     public void onAbort() {
 *         // the obtain failed.
 *     }
 * }).addFutureEventListener(new FutureEventListener() {
 *     public void onSuccess(I resource) {
 *         // the try obtain succeed. but the obtain has not been confirmed or aborted.
 *         // execute the transaction to confirm if it could complete obtain
 *         txn.execute();
 *     }
 *     public void onFailure(Throwable t) {
 *         // handle the failure of try obtain
 *     }
 * });
 *
 * }
*/ public interface Allocator extends AsyncCloseable, AsyncDeleteable { /** * Issue allocation request to allocate I. * The implementation should be non-blocking call. * * @throws IOException * if fail to request allocating a I. */ void allocate() throws IOException; /** * Try obtaining an I in a given transaction T. The object obtained is tentative. * Whether the object is obtained or aborted is determined by the result of the execution. You could * register a listener under this `tryObtain` operation to know whether the object is obtained or * aborted. * *

* It is a typical two-phases operation on obtaining a resource from allocator. * The future returned by this method acts as a `prepare` operation, the resource is tentative obtained * from the allocator. The execution of the txn acts as a `commit` operation, the resource is confirmed * to be obtained by this transaction. listener is for the whole completion of the obtain. *

* listener is only triggered after `prepare` succeed. if `prepare` failed, no actions will * happen to the listener. * * @param txn * transaction. * @return future result returning I that would be obtained under transaction txn. */ Future tryObtain(Transaction txn, OpListener listener); }