Search in sources :

Example 6 with BlockWorkerInfo

use of alluxio.client.block.BlockWorkerInfo in project alluxio by Alluxio.

the class DeterministicHashPolicy method getWorker.

@Override
public WorkerNetAddress getWorker(GetWorkerOptions options) {
    List<BlockWorkerInfo> workerInfos = Lists.newArrayList(options.getBlockWorkerInfos());
    Collections.sort(workerInfos, new Comparator<BlockWorkerInfo>() {

        @Override
        public int compare(BlockWorkerInfo o1, BlockWorkerInfo o2) {
            return o1.getNetAddress().toString().compareToIgnoreCase(o2.getNetAddress().toString());
        }
    });
    HashMap<WorkerNetAddress, BlockWorkerInfo> blockWorkerInfoMap = new HashMap<>();
    for (BlockWorkerInfo workerInfo : options.getBlockWorkerInfos()) {
        blockWorkerInfoMap.put(workerInfo.getNetAddress(), workerInfo);
    }
    List<WorkerNetAddress> workers = new ArrayList<>();
    // Try the next one if the worker mapped from the blockId doesn't work until all the workers
    // are examined.
    int index = (int) (options.getBlockId() % (long) workerInfos.size());
    for (BlockWorkerInfo blockWorkerInfoUnused : workerInfos) {
        WorkerNetAddress candidate = workerInfos.get(index).getNetAddress();
        BlockWorkerInfo workerInfo = blockWorkerInfoMap.get(candidate);
        if (workerInfo != null && workerInfo.getCapacityBytes() >= options.getBlockSize()) {
            workers.add(candidate);
            if (workers.size() >= mShards) {
                break;
            }
        }
        index = (index + 1) % workerInfos.size();
    }
    return workers.isEmpty() ? null : workers.get(mRandom.nextInt(workers.size()));
}
Also used : HashMap(java.util.HashMap) WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) ArrayList(java.util.ArrayList)

Example 7 with BlockWorkerInfo

use of alluxio.client.block.BlockWorkerInfo in project alluxio by Alluxio.

the class RoundRobinPolicy method getWorkerForNextBlock.

/**
   * The policy uses the first fetch of worker info list as the base, and visits each of them in a
   * round-robin manner in the subsequent calls. The policy doesn't assume the list of worker info
   * in the subsequent calls has the same order from the first, and it will skip the workers that
   * are no longer active.
   *
   * @param workerInfoList the info of the active workers
   * @param blockSizeBytes the size of the block in bytes
   * @return the address of the worker to write to
   */
@Override
public WorkerNetAddress getWorkerForNextBlock(Iterable<BlockWorkerInfo> workerInfoList, long blockSizeBytes) {
    if (!mInitialized) {
        mWorkerInfoList = Lists.newArrayList(workerInfoList);
        Collections.shuffle(mWorkerInfoList);
        mIndex = 0;
        mInitialized = true;
    }
    // at most try all the workers
    for (int i = 0; i < mWorkerInfoList.size(); i++) {
        WorkerNetAddress candidate = mWorkerInfoList.get(mIndex).getNetAddress();
        BlockWorkerInfo workerInfo = findBlockWorkerInfo(workerInfoList, candidate);
        mIndex = (mIndex + 1) % mWorkerInfoList.size();
        if (workerInfo != null && workerInfo.getCapacityBytes() >= blockSizeBytes) {
            return candidate;
        }
    }
    return null;
}
Also used : WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo)

Example 8 with BlockWorkerInfo

use of alluxio.client.block.BlockWorkerInfo in project alluxio by Alluxio.

the class DeterministicHashPolicyTest method before.

@Before
public void before() {
    mWorkerInfos.clear();
    mWorkerInfos.add(new BlockWorkerInfo(new WorkerNetAddress().setHost("worker1").setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), Constants.GB, 0));
    mWorkerInfos.add(new BlockWorkerInfo(new WorkerNetAddress().setHost("worker2").setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), 2 * (long) Constants.GB, 0));
    mWorkerInfos.add(new BlockWorkerInfo(new WorkerNetAddress().setHost("worker3").setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), 3 * (long) Constants.GB, 0));
    mWorkerInfos.add(new BlockWorkerInfo(new WorkerNetAddress().setHost("worker4").setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), 3 * (long) Constants.GB, 0));
}
Also used : WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) Before(org.junit.Before)

Example 9 with BlockWorkerInfo

use of alluxio.client.block.BlockWorkerInfo in project alluxio by Alluxio.

the class FileInStreamTest method before.

/**
   * Sets up the context and streams before a test runs.
   *
   * @throws AlluxioException when the worker ufs operations fail
   * @throws IOException when the read and write streams fail
   */
@Before
public void before() throws Exception {
    mInfo = new FileInfo().setBlockSizeBytes(BLOCK_LENGTH).setLength(FILE_LENGTH);
    mDelegateUfsOps = Configuration.getBoolean(PropertyKey.USER_UFS_DELEGATION_ENABLED);
    ClientTestUtils.setSmallBufferSizes();
    mContext = PowerMockito.mock(FileSystemContext.class);
    mBlockStore = Mockito.mock(AlluxioBlockStore.class);
    PowerMockito.mockStatic(AlluxioBlockStore.class);
    PowerMockito.when(AlluxioBlockStore.create(mContext)).thenReturn(mBlockStore);
    PowerMockito.when(mBlockStore.getWorkerInfoList()).thenReturn(new ArrayList<BlockWorkerInfo>());
    Mockito.mock(StreamFactory.class);
    PowerMockito.mockStatic(StreamFactory.class);
    // Set up BufferedBlockInStreams and caching streams
    mCacheStreams = new ArrayList<>();
    List<Long> blockIds = new ArrayList<>();
    for (int i = 0; i < NUM_STREAMS; i++) {
        blockIds.add((long) i);
        mCacheStreams.add(new TestBufferedBlockOutStream(i, getBlockLength(i), mContext));
        Mockito.when(mBlockStore.getInStream(Mockito.eq((long) i), Mockito.any(InStreamOptions.class))).thenAnswer(new Answer<BufferedBlockInStream>() {

            @Override
            public BufferedBlockInStream answer(InvocationOnMock invocation) throws Throwable {
                long i = (Long) invocation.getArguments()[0];
                byte[] input = BufferUtils.getIncreasingByteArray((int) (i * BLOCK_LENGTH), (int) getBlockLength((int) i));
                return new TestBufferedBlockInStream(i, input);
            }
        });
        Mockito.when(mBlockStore.getOutStream(Mockito.eq((long) i), Mockito.anyLong(), Mockito.any(WorkerNetAddress.class), Mockito.any(OutStreamOptions.class))).thenAnswer(new Answer<BufferedBlockOutStream>() {

            @Override
            public BufferedBlockOutStream answer(InvocationOnMock invocation) throws Throwable {
                long i = (Long) invocation.getArguments()[0];
                return mCacheStreams.get((int) i).isClosed() ? null : mCacheStreams.get((int) i);
            }
        });
    }
    mInfo.setBlockIds(blockIds);
    mStatus = new URIStatus(mInfo);
    mTestStream = new FileInStream(mStatus, InStreamOptions.defaults().setReadType(ReadType.CACHE_PROMOTE).setCachePartiallyReadBlock(false), mContext);
}
Also used : TestBufferedBlockOutStream(alluxio.client.block.TestBufferedBlockOutStream) BufferedBlockInStream(alluxio.client.block.BufferedBlockInStream) TestBufferedBlockInStream(alluxio.client.block.TestBufferedBlockInStream) ArrayList(java.util.ArrayList) TestBufferedBlockOutStream(alluxio.client.block.TestBufferedBlockOutStream) BufferedBlockOutStream(alluxio.client.block.BufferedBlockOutStream) TestBufferedBlockInStream(alluxio.client.block.TestBufferedBlockInStream) InStreamOptions(alluxio.client.file.options.InStreamOptions) OutStreamOptions(alluxio.client.file.options.OutStreamOptions) FileInfo(alluxio.wire.FileInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) AlluxioBlockStore(alluxio.client.block.AlluxioBlockStore) Before(org.junit.Before)

Example 10 with BlockWorkerInfo

use of alluxio.client.block.BlockWorkerInfo in project alluxio by Alluxio.

the class LocalFirstAvoidEvictionPolicy method getWorkerForNextBlock.

@Override
public WorkerNetAddress getWorkerForNextBlock(Iterable<BlockWorkerInfo> workerInfoList, long blockSizeBytes) {
    // try the local host first
    WorkerNetAddress localWorkerNetAddress = null;
    for (BlockWorkerInfo workerInfo : workerInfoList) {
        if (workerInfo.getNetAddress().getHost().equals(mLocalHostName)) {
            localWorkerNetAddress = workerInfo.getNetAddress();
            if (getAvailableBytes(workerInfo) >= blockSizeBytes) {
                return localWorkerNetAddress;
            }
        }
    }
    // otherwise randomly pick a worker that has enough availability
    List<BlockWorkerInfo> shuffledWorkers = Lists.newArrayList(workerInfoList);
    Collections.shuffle(shuffledWorkers);
    for (BlockWorkerInfo workerInfo : shuffledWorkers) {
        if (getAvailableBytes(workerInfo) >= blockSizeBytes) {
            return workerInfo.getNetAddress();
        }
    }
    if (localWorkerNetAddress == null && shuffledWorkers.size() > 0) {
        return shuffledWorkers.get(0).getNetAddress();
    }
    return localWorkerNetAddress;
}
Also used : WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo)

Aggregations

BlockWorkerInfo (alluxio.client.block.BlockWorkerInfo)18 WorkerNetAddress (alluxio.wire.WorkerNetAddress)16 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)9 Before (org.junit.Before)3 AlluxioBlockStore (alluxio.client.block.AlluxioBlockStore)2 BufferedBlockOutStream (alluxio.client.block.BufferedBlockOutStream)2 TestBufferedBlockOutStream (alluxio.client.block.TestBufferedBlockOutStream)2 OutStreamOptions (alluxio.client.file.options.OutStreamOptions)2 FileInfo (alluxio.wire.FileInfo)2 HashMap (java.util.HashMap)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 AlluxioURI (alluxio.AlluxioURI)1 BufferedBlockInStream (alluxio.client.block.BufferedBlockInStream)1 TestBufferedBlockInStream (alluxio.client.block.TestBufferedBlockInStream)1 CreateUfsFileOptions (alluxio.client.file.options.CreateUfsFileOptions)1 InStreamOptions (alluxio.client.file.options.InStreamOptions)1 AlluxioException (alluxio.exception.AlluxioException)1 CreateOptions (alluxio.underfs.options.CreateOptions)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1