1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.job;
2021import com.facebook.swift.codec.ThriftCodecManager;
22import com.facebook.swift.service.ThriftEventHandler;
23import com.facebook.swift.service.ThriftServer;
24import com.facebook.swift.service.ThriftServerConfig;
25import com.facebook.swift.service.ThriftServiceProcessor;
26import org.apache.giraph.conf.GiraphConfiguration;
27import org.apache.giraph.conf.IntConfOption;
28import org.apache.giraph.conf.StrConfOption;
2930import java.net.UnknownHostException;
31import java.util.ArrayList;
32import java.util.List;
3334importstatic com.google.common.base.Preconditions.checkNotNull;
3536/**37 * Manages the life cycle of the Thrift server started on the client.38 */39publicclassClientThriftServer {
40/**41 * The client can run a Thrift server (e.g. job progress service).42 * This is the host of the Thrift server.43 */44publicstaticfinalStrConfOption CLIENT_THRIFT_SERVER_HOST =
45newStrConfOption("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 */51publicstaticfinalIntConfOption CLIENT_THRIFT_SERVER_PORT =
52newIntConfOption("giraph.client.thrift.server.port", -1,
53"Port on which the client Thrift server runs (if enabled)");
5455/** Thrift server that is intended to run on the client */56privatefinal ThriftServer clientThriftServer;
5758/**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 start63 */64publicClientThriftServer(GiraphConfiguration conf,
65 List<?> services) {
66 checkNotNull(conf, "conf is null");
67 checkNotNull(services, "services is null");
6869 ThriftServiceProcessor processor =
70new ThriftServiceProcessor(new ThriftCodecManager(),
71new ArrayList<ThriftEventHandler>(),
72 services);
73 clientThriftServer =
74new ThriftServer(processor, new ThriftServerConfig());
75 clientThriftServer.start();
76try {
77 CLIENT_THRIFT_SERVER_HOST.set(
78 conf,
79 conf.getLocalHostname());
80 } catch (UnknownHostException e) {
81thrownew IllegalStateException("Unable to get host information", e);
82 }
83 CLIENT_THRIFT_SERVER_PORT.set(conf, clientThriftServer.getPort());
84 }
8586/**87 * Stop the Thrift server.88 */89publicvoid stopThriftServer() {
90this.clientThriftServer.close();
91 }
92 }