1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
38
39 public class ClientThriftServer {
40
41
42
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
49
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
56 private final ThriftServer clientThriftServer;
57
58
59
60
61
62
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
88
89 public void stopThriftServer() {
90 this.clientThriftServer.close();
91 }
92 }