[ 
https://issues.apache.org/jira/browse/BEAM-9577?focusedWorklogId=424507&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-424507
 ]

ASF GitHub Bot logged work on BEAM-9577:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 17/Apr/20 23:27
            Start Date: 17/Apr/20 23:27
    Worklog Time Spent: 10m 
      Work Description: lukecwik commented on pull request #11342: [BEAM-9577] 
New artifact staging and retrieval service for Java.
URL: https://github.com/apache/beam/pull/11342#discussion_r410513072
 
 

 ##########
 File path: 
runners/java-fn-execution/src/test/java/org/apache/beam/runners/fnexecution/artifact/ArtifactStagingServiceTest.java
 ##########
 @@ -0,0 +1,189 @@
+/*
+ * 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 org.apache.beam.runners.fnexecution.artifact;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import org.apache.beam.model.jobmanagement.v1.ArtifactApi;
+import org.apache.beam.model.jobmanagement.v1.ArtifactRetrievalServiceGrpc;
+import org.apache.beam.model.jobmanagement.v1.ArtifactStagingServiceGrpc;
+import org.apache.beam.model.pipeline.v1.RunnerApi;
+import org.apache.beam.runners.fnexecution.GrpcFnServer;
+import org.apache.beam.runners.fnexecution.InProcessServerFactory;
+import org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.grpc.v1p26p0.io.grpc.ManagedChannel;
+import 
org.apache.beam.vendor.grpc.v1p26p0.io.grpc.inprocess.InProcessChannelBuilder;
+import org.apache.beam.vendor.grpc.v1p26p0.io.grpc.stub.StreamObserver;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Strings;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class ArtifactStagingServiceTest {
+  private static final int TEST_BUFFER_SIZE = 1 << 10;
+  private GrpcFnServer<ArtifactStagingService> stagingServer;
+  private ArtifactStagingService stagingService;
+  private GrpcFnServer<ArtifactRetrievalService> retrievalServer;
+  private ArtifactRetrievalService retrievalService;
+  private ArtifactStagingServiceGrpc.ArtifactStagingServiceStub stagingStub;
+  private ArtifactRetrievalServiceGrpc.ArtifactRetrievalServiceBlockingStub 
retrievalBlockingStub;
+  private Path stagingDir;
+  @Rule public TemporaryFolder tempFolder = new TemporaryFolder();
+
+  @Before
+  public void setUp() throws Exception {
+    stagingDir = tempFolder.newFolder("staging").toPath();
+    stagingService =
+        new ArtifactStagingService(
+            ArtifactStagingService.beamFilesystemArtifactDestinationProvider(
+                stagingDir.toString()));
+    stagingServer =
+        GrpcFnServer.allocatePortAndCreateFor(stagingService, 
InProcessServerFactory.create());
+    ManagedChannel stagingChannel =
+        
InProcessChannelBuilder.forName(stagingServer.getApiServiceDescriptor().getUrl()).build();
+    stagingStub = ArtifactStagingServiceGrpc.newStub(stagingChannel);
+
+    retrievalService = new ArtifactRetrievalService(TEST_BUFFER_SIZE);
+    retrievalServer =
+        GrpcFnServer.allocatePortAndCreateFor(retrievalService, 
InProcessServerFactory.create());
+    ManagedChannel retrievalChannel =
+        
InProcessChannelBuilder.forName(retrievalServer.getApiServiceDescriptor().getUrl()).build();
+    retrievalBlockingStub = 
ArtifactRetrievalServiceGrpc.newBlockingStub(retrievalChannel);
+  }
+
+  private static class FakeArtifactRetrievalService extends 
ArtifactRetrievalService {
+
+    @Override
+    public void resolveArtifacts(
+        ArtifactApi.ResolveArtifactsRequest request,
+        StreamObserver<ArtifactApi.ResolveArtifactsResponse> responseObserver) 
{
+      ArtifactApi.ResolveArtifactsResponse.Builder response =
+          ArtifactApi.ResolveArtifactsResponse.newBuilder();
+      for (RunnerApi.ArtifactInformation artifact : 
request.getArtifactsList()) {
+        if (artifact.getTypeUrn().equals("resolved")) {
+          response.addReplacements(artifact);
+        } else if (artifact.getTypeUrn().equals("unresolved")) {
+          
response.addReplacements(artifact.toBuilder().setTypeUrn("resolved").build());
+        } else {
+          throw new UnsupportedOperationException(artifact.getTypeUrn());
+        }
+      }
+      responseObserver.onNext(response.build());
+      responseObserver.onCompleted();
+    }
+
+    @Override
+    public void getArtifact(
+        ArtifactApi.GetArtifactRequest request,
+        StreamObserver<ArtifactApi.GetArtifactResponse> responseObserver) {
+      if (request.getArtifact().getTypeUrn().equals("resolved")) {
+        ByteString data = request.getArtifact().getTypePayload();
+        responseObserver.onNext(
+            
ArtifactApi.GetArtifactResponse.newBuilder().setData(data.substring(0, 
1)).build());
+        responseObserver.onNext(
+            
ArtifactApi.GetArtifactResponse.newBuilder().setData(data.substring(1)).build());
+        responseObserver.onCompleted();
+      } else {
+        throw new 
UnsupportedOperationException(request.getArtifact().getTypeUrn());
+      }
+    }
+
+    public static RunnerApi.ArtifactInformation resolvedArtifact(String 
contents) {
+      return RunnerApi.ArtifactInformation.newBuilder()
+          .setTypeUrn("resolved")
+          .setTypePayload(ByteString.copyFromUtf8(contents))
+          .setRoleUrn(contents)
+          .build();
+    }
+
+    public static RunnerApi.ArtifactInformation unresolvedArtifact(String 
contents) {
+      return RunnerApi.ArtifactInformation.newBuilder()
+          .setTypeUrn("unresolved")
+          .setTypePayload(ByteString.copyFromUtf8(contents))
+          .setRoleUrn(contents)
+          .build();
+    }
+  }
+
+  private String getArtifact(RunnerApi.ArtifactInformation artifact) {
+    ByteString all = ByteString.EMPTY;
+    Iterator<ArtifactApi.GetArtifactResponse> response =
+        retrievalBlockingStub.getArtifact(
+            
ArtifactApi.GetArtifactRequest.newBuilder().setArtifact(artifact).build());
+    while (response.hasNext()) {
+      all = all.concat(response.next().getData());
+    }
+    return all.toStringUtf8();
+  }
+
+  @Test
+  public void testStageArtifacts() throws IOException, InterruptedException {
+    List<String> contentsList =
+        ImmutableList.of("a", "bb", Strings.repeat("xyz", TEST_BUFFER_SIZE * 3 
/ 4));
+    stagingService.registerJob(
+        "stagingToken",
+        ImmutableMap.of(
+            "env1",
+            Lists.transform(contentsList, 
FakeArtifactRetrievalService::resolvedArtifact),
+            "env2",
+            Lists.transform(contentsList, 
FakeArtifactRetrievalService::unresolvedArtifact)));
+    ArtifactStagingService.offer(new FakeArtifactRetrievalService(), 
stagingStub, "stagingToken");
+    Map<String, List<RunnerApi.ArtifactInformation>> staged =
+        stagingService.getStagedArtifacts("stagingToken");
+    assertEquals(2, staged.size());
+    checkArtifacts(contentsList, staged.get("env1"));
+    checkArtifacts(contentsList, staged.get("env2"));
+  }
+
+  private void checkArtifacts(
+      Collection<String> expetedContents, List<RunnerApi.ArtifactInformation> 
staged) {
 
 Review comment:
   `expetedContents` -> `expectedContents`
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 424507)
    Time Spent: 16h 50m  (was: 16h 40m)

> Update artifact staging and retrieval protocols to be dependency aware.
> -----------------------------------------------------------------------
>
>                 Key: BEAM-9577
>                 URL: https://issues.apache.org/jira/browse/BEAM-9577
>             Project: Beam
>          Issue Type: Improvement
>          Components: beam-model
>            Reporter: Robert Bradshaw
>            Assignee: Robert Bradshaw
>            Priority: Major
>          Time Spent: 16h 50m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to