Search in sources :

Example 36 with WorkerNetAddress

use of alluxio.wire.WorkerNetAddress in project alluxio by Alluxio.

the class AlluxioBlockStore method getOutStream.

/**
 * Gets a stream to write data to a block based on the options. The stream can only be backed by
 * Alluxio storage.
 *
 * @param blockId the block to write
 * @param blockSize the standard block size to write
 * @param options the output stream option
 * @return a {@link BlockOutStream} which can be used to write data to the block in a streaming
 *         fashion
 */
public BlockOutStream getOutStream(long blockId, long blockSize, OutStreamOptions options) throws IOException {
    WorkerNetAddress address;
    BlockLocationPolicy locationPolicy = Preconditions.checkNotNull(options.getLocationPolicy(), PreconditionMessage.BLOCK_WRITE_LOCATION_POLICY_UNSPECIFIED);
    GetWorkerOptions workerOptions = GetWorkerOptions.defaults().setBlockInfo(new BlockInfo().setBlockId(blockId).setLength(blockSize)).setBlockWorkerInfos(new ArrayList<>(mContext.getCachedWorkers()));
    // The number of initial copies depends on the write type: if ASYNC_THROUGH, it is the property
    // "alluxio.user.file.replication.durable" before data has been persisted; otherwise
    // "alluxio.user.file.replication.min"
    int initialReplicas = (options.getWriteType() == WriteType.ASYNC_THROUGH && options.getReplicationDurable() > options.getReplicationMin()) ? options.getReplicationDurable() : options.getReplicationMin();
    if (initialReplicas <= 1) {
        address = locationPolicy.getWorker(workerOptions);
        if (address == null) {
            if (mContext.getCachedWorkers().isEmpty()) {
                throw new UnavailableException(ExceptionMessage.NO_WORKER_AVAILABLE.getMessage());
            }
            throw new UnavailableException(ExceptionMessage.NO_SPACE_FOR_BLOCK_ON_WORKER.getMessage(blockSize));
        }
        // TODO(ggezer): Retry on another worker if this has no storage.
        return getOutStream(blockId, blockSize, address, options);
    }
    // Group different block workers by their hostnames
    Map<String, Set<BlockWorkerInfo>> blockWorkersByHost = new HashMap<>();
    for (BlockWorkerInfo blockWorker : workerOptions.getBlockWorkerInfos()) {
        String hostName = blockWorker.getNetAddress().getHost();
        if (blockWorkersByHost.containsKey(hostName)) {
            blockWorkersByHost.get(hostName).add(blockWorker);
        } else {
            blockWorkersByHost.put(hostName, com.google.common.collect.Sets.newHashSet(blockWorker));
        }
    }
    // Select N workers on different hosts where N is the value of initialReplicas for this block
    List<WorkerNetAddress> workerAddressList = new ArrayList<>();
    List<BlockWorkerInfo> updatedInfos = Lists.newArrayList(workerOptions.getBlockWorkerInfos());
    for (int i = 0; i < initialReplicas; i++) {
        address = locationPolicy.getWorker(workerOptions);
        if (address == null) {
            break;
        }
        workerAddressList.add(address);
        updatedInfos.removeAll(blockWorkersByHost.get(address.getHost()));
        workerOptions.setBlockWorkerInfos(updatedInfos);
    }
    if (workerAddressList.size() < initialReplicas) {
        throw new alluxio.exception.status.ResourceExhaustedException(String.format("Not enough workers for replications, %d workers selected but %d required", workerAddressList.size(), initialReplicas));
    }
    return BlockOutStream.createReplicatedBlockOutStream(mContext, blockId, blockSize, workerAddressList, options);
}
Also used : Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) HashMap(java.util.HashMap) UnavailableException(alluxio.exception.status.UnavailableException) ArrayList(java.util.ArrayList) GetWorkerOptions(alluxio.client.block.policy.options.GetWorkerOptions) BlockLocationPolicy(alluxio.client.block.policy.BlockLocationPolicy) WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockInfo(alluxio.wire.BlockInfo)

Example 37 with WorkerNetAddress

use of alluxio.wire.WorkerNetAddress in project alluxio by Alluxio.

the class LocalFirstAvoidEvictionPolicy method getWorker.

@Override
public WorkerNetAddress getWorker(GetWorkerOptions options) {
    List<BlockWorkerInfo> allWorkers = Lists.newArrayList(options.getBlockWorkerInfos());
    // Prefer workers with enough availability.
    List<BlockWorkerInfo> workers = allWorkers.stream().filter(worker -> getAvailableBytes(worker) >= options.getBlockInfo().getLength()).collect(Collectors.toList());
    if (workers.isEmpty()) {
        workers = allWorkers;
    }
    GetWorkerOptions filteredWorkers = GetWorkerOptions.defaults().setBlockInfo(options.getBlockInfo()).setBlockWorkerInfos(workers);
    return mPolicy.getWorker(filteredWorkers);
}
Also used : List(java.util.List) Lists(com.google.common.collect.Lists) WorkerNetAddress(alluxio.wire.WorkerNetAddress) AlluxioConfiguration(alluxio.conf.AlluxioConfiguration) MoreObjects(com.google.common.base.MoreObjects) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) TieredIdentity(alluxio.wire.TieredIdentity) ThreadSafe(javax.annotation.concurrent.ThreadSafe) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Objects(com.google.common.base.Objects) GetWorkerOptions(alluxio.client.block.policy.options.GetWorkerOptions) Collectors(java.util.stream.Collectors) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) GetWorkerOptions(alluxio.client.block.policy.options.GetWorkerOptions)

Example 38 with WorkerNetAddress

use of alluxio.wire.WorkerNetAddress in project alluxio by Alluxio.

the class MostAvailableFirstPolicy method getWorker.

/**
 * The policy returns null if no worker is qualified.
 */
@Override
public WorkerNetAddress getWorker(GetWorkerOptions options) {
    long mostAvailableBytes = -1;
    WorkerNetAddress result = null;
    for (BlockWorkerInfo workerInfo : options.getBlockWorkerInfos()) {
        if (workerInfo.getCapacityBytes() - workerInfo.getUsedBytes() > mostAvailableBytes) {
            mostAvailableBytes = workerInfo.getCapacityBytes() - workerInfo.getUsedBytes();
            result = workerInfo.getNetAddress();
        }
    }
    return result;
}
Also used : WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo)

Example 39 with WorkerNetAddress

use of alluxio.wire.WorkerNetAddress in project alluxio by Alluxio.

the class FileOutStreamTest method before.

/**
 * Sets up the different contexts and clients before a test runs.
 */
@Before
public void before() throws Exception {
    GroupMappingServiceTestUtils.resetCache();
    ClientTestUtils.setSmallBufferSizes(sConf);
    mClientContext = ClientContext.create(sConf);
    // PowerMock enums and final classes
    mFileSystemContext = PowerMockito.mock(FileSystemContext.class);
    when(mFileSystemContext.getClientContext()).thenReturn(mClientContext);
    when(mFileSystemContext.getClusterConf()).thenReturn(sConf);
    when(mFileSystemContext.getPathConf(any(AlluxioURI.class))).thenReturn(sConf);
    mBlockStore = PowerMockito.mock(AlluxioBlockStore.class);
    mFileSystemMasterClient = PowerMockito.mock(FileSystemMasterClient.class);
    PowerMockito.mockStatic(AlluxioBlockStore.class);
    PowerMockito.when(AlluxioBlockStore.create(mFileSystemContext)).thenReturn(mBlockStore);
    when(mFileSystemContext.acquireMasterClientResource()).thenReturn(new DummyCloseableResource<>(mFileSystemMasterClient));
    when(mFileSystemMasterClient.getStatus(any(AlluxioURI.class), any(GetStatusPOptions.class))).thenReturn(new URIStatus(new FileInfo()));
    // Return sequentially increasing numbers for new block ids
    when(mFileSystemMasterClient.getNewBlockIdForFile(FILE_NAME)).thenAnswer(new Answer<Long>() {

        private long mCount = 0;

        @Override
        public Long answer(InvocationOnMock invocation) throws Throwable {
            return mCount++;
        }
    });
    // Set up out streams. When they are created, add them to outStreamMap
    final Map<Long, TestBlockOutStream> outStreamMap = new HashMap<>();
    when(mBlockStore.getOutStream(anyLong(), eq(BLOCK_LENGTH), any(OutStreamOptions.class))).thenAnswer(new Answer<TestBlockOutStream>() {

        @Override
        public TestBlockOutStream answer(InvocationOnMock invocation) throws Throwable {
            Long blockId = invocation.getArgument(0, Long.class);
            if (!outStreamMap.containsKey(blockId)) {
                TestBlockOutStream newStream = new TestBlockOutStream(ByteBuffer.allocate(1000), BLOCK_LENGTH);
                outStreamMap.put(blockId, newStream);
            }
            return outStreamMap.get(blockId);
        }
    });
    BlockWorkerInfo workerInfo = new BlockWorkerInfo(new WorkerNetAddress().setHost("localhost").setTieredIdentity(TieredIdentityFactory.fromString("node=localhost", sConf)).setRpcPort(1).setDataPort(2).setWebPort(3), Constants.GB, 0);
    when(mFileSystemContext.getCachedWorkers()).thenReturn(Lists.newArrayList(workerInfo));
    mAlluxioOutStreamMap = outStreamMap;
    // Create an under storage stream so that we can check whether it has been flushed
    final AtomicBoolean underStorageFlushed = new AtomicBoolean(false);
    mUnderStorageOutputStream = new TestUnderFileSystemFileOutStream(ByteBuffer.allocate(5000)) {

        @Override
        public void flush() throws IOException {
            super.flush();
            underStorageFlushed.set(true);
        }
    };
    mUnderStorageFlushed = underStorageFlushed;
    PowerMockito.mockStatic(UnderFileSystemFileOutStream.class);
    PowerMockito.when(UnderFileSystemFileOutStream.create(any(FileSystemContext.class), any(WorkerNetAddress.class), any(OutStreamOptions.class))).thenReturn(mUnderStorageOutputStream);
    OutStreamOptions options = OutStreamOptions.defaults(mClientContext).setBlockSizeBytes(BLOCK_LENGTH).setWriteType(WriteType.CACHE_THROUGH).setUfsPath(FILE_NAME.getPath());
    mTestStream = createTestStream(FILE_NAME, options);
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) GetStatusPOptions(alluxio.grpc.GetStatusPOptions) TestUnderFileSystemFileOutStream(alluxio.client.block.stream.TestUnderFileSystemFileOutStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OutStreamOptions(alluxio.client.file.options.OutStreamOptions) FileInfo(alluxio.wire.FileInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) WorkerNetAddress(alluxio.wire.WorkerNetAddress) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) TestBlockOutStream(alluxio.client.block.stream.TestBlockOutStream) AlluxioBlockStore(alluxio.client.block.AlluxioBlockStore) AlluxioURI(alluxio.AlluxioURI) Before(org.junit.Before)

Example 40 with WorkerNetAddress

use of alluxio.wire.WorkerNetAddress in project alluxio by Alluxio.

the class BlockLocationUtilsTest method chooseLocalAccessibleDomainSocket.

@Test
public void chooseLocalAccessibleDomainSocket() throws Exception {
    List<BlockWorkerInfo> workers = new ArrayList<>();
    workers.add(worker(Constants.GB, "node2", "rack2"));
    // create worker info with domain socket path
    BlockWorkerInfo workerWithDomainSocket = worker(Constants.GB, "node3", "rack3");
    String domainSocketPath = "/tmp/domain/uuid-node3";
    workerWithDomainSocket.getNetAddress().setDomainSocketPath(domainSocketPath);
    workers.add(workerWithDomainSocket);
    // mock NettyUtils
    PowerMockito.mockStatic(NettyUtils.class);
    when(NettyUtils.isDomainSocketAccessible(eq(workerWithDomainSocket.getNetAddress()), any(AlluxioConfiguration.class))).thenReturn(true);
    // choose worker with domain socket accessible ignoring rack
    InstancedConfiguration conf = ConfigurationTestUtils.defaults();
    conf.set(PropertyKey.WORKER_DATA_SERVER_DOMAIN_SOCKET_AS_UUID, true);
    List<WorkerNetAddress> addresses = workers.stream().map(worker -> worker.getNetAddress()).filter(Objects::nonNull).collect(Collectors.toList());
    Optional<Pair<WorkerNetAddress, Boolean>> chosen = BlockLocationUtils.nearest(TieredIdentityFactory.fromString("node=node1,rack=rack2", conf), addresses, conf);
    assertTrue(chosen.isPresent());
    assertTrue(chosen.get().getSecond());
    assertEquals(domainSocketPath, chosen.get().getFirst().getDomainSocketPath());
    assertEquals("node3", chosen.get().getFirst().getHost());
}
Also used : InstancedConfiguration(alluxio.conf.InstancedConfiguration) WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) ArrayList(java.util.ArrayList) AlluxioConfiguration(alluxio.conf.AlluxioConfiguration) Pair(alluxio.collections.Pair) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

WorkerNetAddress (alluxio.wire.WorkerNetAddress)117 Test (org.junit.Test)63 BlockWorkerInfo (alluxio.client.block.BlockWorkerInfo)43 ArrayList (java.util.ArrayList)38 BlockInfo (alluxio.wire.BlockInfo)36 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 URIStatus (alluxio.client.file.URIStatus)22 IOException (java.io.IOException)21 AlluxioURI (alluxio.AlluxioURI)20 FileBlockInfo (alluxio.wire.FileBlockInfo)20 InStreamOptions (alluxio.client.file.options.InStreamOptions)19 FileInfo (alluxio.wire.FileInfo)16 GetWorkerOptions (alluxio.client.block.policy.options.GetWorkerOptions)15 List (java.util.List)14 FileSystemContext (alluxio.client.file.FileSystemContext)12 OpenFilePOptions (alluxio.grpc.OpenFilePOptions)12 BlockLocation (alluxio.wire.BlockLocation)12 BlockInStream (alluxio.client.block.stream.BlockInStream)11 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)11 Before (org.junit.Before)11