This project has retired. For details please refer to its Attic page.
ClientThriftServer xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.job;
20  
21  import com.facebook.swift.codec.ThriftCodecManager;
22  import com.facebook.swift.service.ThriftEventHandler;
23  import com.facebook.swift.service.ThriftServer;
24  import com.facebook.swift.service.ThriftServerConfig;
25  import com.facebook.swift.service.ThriftServiceProcessor;
26  import org.apache.giraph.conf.GiraphConfiguration;
27  import org.apache.giraph.conf.IntConfOption;
28  import org.apache.giraph.conf.StrConfOption;
29  
30  import java.net.UnknownHostException;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import static com.google.common.base.Preconditions.checkNotNull;
35  
36  /**
37   * Manages the life cycle of the Thrift server started on the client.
38   */
39  public class ClientThriftServer {
40    /**
41     * The client can run a Thrift server (e.g. job progress service).
42     * This is the host of the Thrift server.
43     */
44    public static final StrConfOption CLIENT_THRIFT_SERVER_HOST =
45        new StrConfOption("giraph.client.thrift.server.host", null,
46            "Host on which the client Thrift server runs (if enabled)");
47    /**
48     * The client can run a Thrift server (e.g. job progress service).
49     * This is the port of the Thrift server.
50     */
51    public static final IntConfOption CLIENT_THRIFT_SERVER_PORT =
52        new IntConfOption("giraph.client.thrift.server.port", -1,
53            "Port on which the client Thrift server runs (if enabled)");
54  
55    /** Thrift server that is intended to run on the client */
56    private final ThriftServer clientThriftServer;
57  
58    /**
59     * Create and start the Thrift server.
60     *
61     * @param conf Giraph conf to set the host and ports for.
62     * @param services Services to start
63     */
64    public ClientThriftServer(GiraphConfiguration conf,
65                              List<?> services) {
66      checkNotNull(conf, "conf is null");
67      checkNotNull(services, "services is null");
68  
69      ThriftServiceProcessor processor =
70          new ThriftServiceProcessor(new ThriftCodecManager(),
71                                     new ArrayList<ThriftEventHandler>(),
72                                     services);
73      clientThriftServer =
74          new ThriftServer(processor, new ThriftServerConfig());
75      clientThriftServer.start();
76      try {
77        CLIENT_THRIFT_SERVER_HOST.set(
78            conf,
79            conf.getLocalHostname());
80      } catch (UnknownHostException e) {
81        throw new IllegalStateException("Unable to get host information", e);
82      }
83      CLIENT_THRIFT_SERVER_PORT.set(conf, clientThriftServer.getPort());
84    }
85  
86    /**
87     * Stop the Thrift server.
88     */
89    public void stopThriftServer() {
90      this.clientThriftServer.close();
91    }
92  }