/
Client.java
233 lines (204 loc) · 7.16 KB
/
Client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package zingg.client;
import java.io.Serializable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.spark.api.java.JavaSparkContext;
import zingg.client.util.Email;
import zingg.client.util.EmailBody;
/**
* This is the main point of interface with the Zingg matching product.
*
* @author sgoyal
*
*/
public class Client implements Serializable {
private Arguments arguments;
private IZingg zingg;
private ClientOptions options;
public static final Log LOG = LogFactory.getLog(Client.class);
/**
* Construct a client to Zingg using provided arguments and spark master.
* If running locally, set the master to local.
*
* @param args
* - arguments for training and matching
* @throws ZinggClientException
* if issue connecting to master
*/
public Client(Arguments args, ClientOptions options) throws ZinggClientException {
this.options = options;
try {
buildAndSetArguments(args, options);
printAnalyticsBanner(arguments.getCollectMetrics());
setZingg(args, options);
}
catch (Exception e) {
e.printStackTrace();
throw new ZinggClientException("An error has occured while setting up the client" + e.getMessage());
}
}
public void setZingg(Arguments args, ClientOptions options) throws Exception{
JavaSparkContext.jarOfClass(IZinggFactory.class);
IZinggFactory zf = (IZinggFactory) Class.forName("zingg.ZFactory").newInstance();
setZingg(zf.get(ZinggOptions.getByValue(options.get(ClientOptions.PHASE).value.trim())));
}
public void setZingg(IZingg zingg) {
this.zingg = zingg;
}
public void buildAndSetArguments(Arguments args, ClientOptions options) {
int jobId = new Long(System.currentTimeMillis()).intValue();
if (options.get(options.JOBID)!= null) {
LOG.info("Using job id from command line");
String j = options.get(options.JOBID).value;
jobId = Integer.parseInt(j);
args.setJobId(jobId);
}
else if (args.getJobId() != -1) {
jobId = args.getJobId();
}
//override value of zinggDir passed from command line
if (options.get(options.ZINGG_DIR)!= null) {
LOG.info("Using zingg dir from command line");
String j = options.get(options.ZINGG_DIR).value;
args.setZinggDir(j);
}
if (options.get(options.MODEL_ID)!= null) {
LOG.info("Using model id from command line");
String j = options.get(options.MODEL_ID).value;
args.setModelId(j);
}
if (options.get(options.COLLECT_METRICS)!= null) {
String j = options.get(options.COLLECT_METRICS).value;
args.setCollectMetrics(Boolean.valueOf(j));
}
if (options.get(ClientOptions.SHOW_CONCISE)!= null) {
String j = options.get(ClientOptions.SHOW_CONCISE).value;
args.setShowConcise(Boolean.valueOf(j));
}
setArguments(args);
}
public static void printBanner() {
String versionStr = "0.3.3";
LOG.info("");
LOG.info("********************************************************");
LOG.info("* Zingg AI *");
LOG.info("* (C) 2021 Zingg.AI *");
LOG.info("********************************************************");
LOG.info("");
LOG.info("using: Zingg v" + versionStr);
LOG.info("");
}
public static void printAnalyticsBanner(boolean collectMetrics) {
if(collectMetrics) {
LOG.info("");
LOG.info("**************************************************************************");
LOG.info("* ** Note about analytics collection by Zingg AI ** *");
LOG.info("* *");
LOG.info("* Please note that Zingg captures a few metrics about application's *");
LOG.info("* runtime parameters. However, no user's personal data or application *");
LOG.info("* data is captured. If you want to switch off this feature, please *");
LOG.info("* set the flag collectMetrics to false in config. For details, please *");
LOG.info("* refer to the Zingg docs (https://docs.zingg.ai/docs/security.html) *");
LOG.info("**************************************************************************");
LOG.info("");
}
else {
LOG.info("");
LOG.info("********************************************************");
LOG.info("* Zingg is not collecting any analytics data *");
LOG.info("********************************************************");
LOG.info("");
}
}
public static void main(String... args) {
printBanner();
Client client = null;
ClientOptions options = null;
try {
for (String a: args) LOG.debug("args " + a);
options = new ClientOptions(args);
if (options.has(options.HELP) || options.has(options.HELP1) || options.get(ClientOptions.PHASE) == null) {
LOG.warn(options.getHelp());
System.exit(0);
}
String phase = options.get(ClientOptions.PHASE).value.trim();
ZinggOptions.verifyPhase(phase);
Arguments arguments = null;
if (options.get(ClientOptions.CONF).value.endsWith("json")) {
arguments = Arguments.createArgumentsFromJSON(options.get(ClientOptions.CONF).value, phase);
}
else if (options.get(ClientOptions.CONF).value.endsWith("env")) {
arguments = Arguments.createArgumentsFromJSONTemplate(options.get(ClientOptions.CONF).value, phase);
}
else {
arguments = Arguments.createArgumentsFromJSONString(options.get(ClientOptions.CONF).value, phase);
}
client = new Client(arguments, options);
client.init();
client.execute();
client.postMetrics();
LOG.warn("Zingg processing has completed");
}
catch(ZinggClientException e) {
if (options != null) {
Email.email(options.get(ClientOptions.EMAIL).value, new EmailBody("Error running Zingg job",
"Zingg Error ",
e.getMessage()));
}
LOG.warn("Apologies for this message. Zingg has encountered an error. "
+ e.getMessage());
if (LOG.isDebugEnabled()) e.printStackTrace();
}
catch( Throwable e) {
if (options != null && options.get(ClientOptions.EMAIL) != null) {
Email.email(options.get(ClientOptions.EMAIL).value, new EmailBody("Error running Zingg job",
"Zingg Error ",
e.getMessage()));
}
LOG.warn("Apologies for this message. Zingg has encountered an error. "
+ e.getMessage());
if (LOG.isDebugEnabled()) e.printStackTrace();
}
finally {
try {
if (client != null) {
client.stop();
}
}
catch(ZinggClientException e) {
if (options != null) {
Email.email(options.get(ClientOptions.EMAIL).value, new EmailBody("Error running Zingg job",
"Zingg Error ",
e.getMessage()));
}
}
}
}
public void init() throws ZinggClientException {
zingg.init(getArguments(), "");
}
/**
* Stop the Spark job running context
*/
public void stop() throws ZinggClientException{
zingg.cleanup();
}
public Arguments getArguments() {
return arguments;
}
public void execute() throws ZinggClientException {
zingg.execute();
}
public void postMetrics() throws ZinggClientException {
zingg.postMetrics();
}
public void setArguments(Arguments args) {
this.arguments = args;
}
public ClientOptions getOptions() {
return options;
}
public void setOptions(ClientOptions options) {
this.options = options;
}
}