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

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

                Author: ASF GitHub Bot
            Created on: 09/Apr/19 18:26
            Start Date: 09/Apr/19 18:26
    Worklog Time Spent: 10m 
      Work Description: TheNeuralBit commented on pull request #8104: 
[BEAM-6872] Add hook for user-defined JVM initialization in workers
URL: https://github.com/apache/beam/pull/8104#discussion_r273640379
 
 

 ##########
 File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/util/BeamWorkerInitializer.java
 ##########
 @@ -0,0 +1,82 @@
+/*
+ * 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.sdk.util;
+
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.util.common.ReflectHelpers;
+
+/**
+ * A service interface for defining one-time initialization for Beam workers.
+ *
+ * <p>Beam workers will use {@code runOnStartup} and {@code 
runBeforeProcessing} to run every
+ * registered implementation's {@code onStartup} and {@code beforeProcessing} 
functions at the
+ * appropriate stage of execution.
+ *
+ * <p>{@link java.util.ServiceLoader} is used to discover implementations of 
{@link
+ * BeamWorkerInitializer}, note that you will need to register your 
implementation with the
+ * appropriate resources to ensure your code is executed. You can use a tool 
like {@link
+ * com.google.auto.service.AutoService} to automate this.
+ */
+@Experimental
+public abstract class BeamWorkerInitializer {
+  /**
+   * Finds all registered implementations of BeamWorkerInitializer and 
executes their {@code
+   * onStartup} methods. Called in worker harness implementations at the very 
beginning of their
+   * main method.
+   */
+  public static void runOnStartup() {
+    for (BeamWorkerInitializer initializer :
+        ReflectHelpers.loadServicesOrdered(BeamWorkerInitializer.class)) {
+      initializer.onStartup();
+    }
+  }
+
+  /**
+   * Finds all registered implementations of BeamWorkerInitializer and 
executes their {@code
+   * beforeProcessing} methods. Called in worker harness implementations after 
initialization but
+   * before beginning to process any data.
+   *
+   * @param options The pipeline options passed to the worker.
+   */
+  public static void runBeforeProcessing(PipelineOptions options) {
+    for (BeamWorkerInitializer initializer :
+        ReflectHelpers.loadServicesOrdered(BeamWorkerInitializer.class)) {
+      initializer.beforeProcessing(options);
+    }
+  }
+
+  /**
+   * Implement onStartup to run some custom initialization immediately after 
the worker begins
 
 Review comment:
   I moved the static methods to 
`org.apache.beam.sdk.fn.BeamWorkerInitializerHelpers` - is that what you had in 
mind? 
 
----------------------------------------------------------------
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:
us...@infra.apache.org


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

    Worklog Id:     (was: 225196)
    Time Spent: 2h 20m  (was: 2h 10m)

> Add hook for user-defined JVM initialization in workers
> -------------------------------------------------------
>
>                 Key: BEAM-6872
>                 URL: https://issues.apache.org/jira/browse/BEAM-6872
>             Project: Beam
>          Issue Type: New Feature
>          Components: runner-dataflow
>            Reporter: Brian Hulette
>            Assignee: Brian Hulette
>            Priority: Minor
>          Time Spent: 2h 20m
>  Remaining Estimate: 0h
>
> Expose an interface for users to run some one-time initialization code when a 
> worker starts up.
> This can be useful for things like overriding the Default ZoneRulesProvider, 
> or setting up custom SSL providers.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to