This repository has been archived by the owner on Apr 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
FalconClient.java
1337 lines (1164 loc) · 65 KB
/
FalconClient.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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.falcon.client;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;
import com.sun.jersey.multipart.FormDataMultiPart;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.util.TrustManagerUtils;
import org.apache.falcon.LifeCycle;
import org.apache.falcon.ExtensionHandler;
import org.apache.falcon.entity.v0.DateValidator;
import org.apache.falcon.entity.v0.Entity;
import org.apache.falcon.entity.v0.EntityType;
import org.apache.falcon.extensions.ExtensionType;
import org.apache.falcon.metadata.RelationshipType;
import org.apache.falcon.resource.APIResult;
import org.apache.falcon.resource.EntityList;
import org.apache.falcon.resource.EntitySummaryResult;
import org.apache.falcon.resource.ExtensionInstanceList;
import org.apache.falcon.resource.ExtensionJobList;
import org.apache.falcon.resource.FeedInstanceResult;
import org.apache.falcon.resource.FeedLookupResult;
import org.apache.falcon.resource.InstanceDependencyResult;
import org.apache.falcon.resource.InstancesResult;
import org.apache.falcon.resource.InstancesSummaryResult;
import org.apache.falcon.resource.LineageGraphResult;
import org.apache.falcon.resource.SchedulableEntityInstanceResult;
import org.apache.falcon.resource.TriageResult;
import org.apache.hadoop.security.authentication.client.AuthenticatedURL;
import org.apache.hadoop.security.authentication.client.KerberosAuthenticator;
import org.apache.hadoop.security.authentication.client.PseudoAuthenticator;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.SecureRandom;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
/**
* Client API to submit and manage Falcon Entities (Cluster, Feed, Process) jobs
* against an Falcon instance.
*/
public class FalconClient extends AbstractFalconClient {
public static final AtomicReference<PrintStream> OUT = new AtomicReference<>(System.out);
public static final Logger LOG = LoggerFactory.getLogger(FalconClient.class);
public static final String WS_HEADER_PREFIX = "header:";
public static final String USER = System.getProperty("user.name");
private static final String AUTH_URL = "api/options?" + PseudoAuthenticator.USER_NAME + "=" + USER;
private static final String PATH = "path";
private static final String COLO = "colo";
private static final String KEY = "key";
private static final String VALUE = "value";
public static final String CLUSTER = "cluster";
private static final String RUN_ID = "runid";
private static final String FORCE = "force";
private static final String SHOW_SCHEDULER = "showScheduler";
private static final String ENTITY_NAME = "name";
private static final String ENTITY_TYPE = "type";
private static final String SKIP_DRYRUN = "skipDryRun";
private static final String FILTER_BY = "filterBy";
private static final String ORDER_BY = "orderBy";
private static final String SORT_ORDER = "sortOrder";
private static final String OFFSET = "offset";
private static final String NUM_RESULTS = "numResults";
private static final String START = "start";
private static final String END = "end";
private static final String INSTANCE_TIME = "instanceTime";
private static final String INSTANCE_STATUS = "instanceStatus";
private static final String PROPERTIES = "properties";
private static final String FIELDS = "fields";
private static final String NAME_SUBSEQUENCE = "nameseq";
private static final String FILTER_TAGS = "tags";
private static final String TAG_KEYWORDS = "tagkeys";
private static final String LIFECYCLE = "lifecycle";
private static final String NUM_INSTANCES = "numInstances";
private static final String ALL_ATTEMPTS = "allAttempts";
private static final String DO_AS_OPT = "doAs";
private static final String JOB_NAME_OPT = "jobName";
public static final String ENTITIES_OPT = "entities";
/**
* Name of the HTTP cookie used for the authentication token between the client and the server.
*/
private static final String AUTH_COOKIE = "hadoop.auth";
private static final String AUTH_COOKIE_EQ = AUTH_COOKIE + "=";
private static final KerberosAuthenticator AUTHENTICATOR = new KerberosAuthenticator();
private static final String TEMPLATE_SUFFIX = "-template.xml";
private static final String PROPERTIES_SUFFIX = ".properties";
private static final HostnameVerifier ALL_TRUSTING_HOSTNAME_VERIFIER = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession sslSession) {
return true;
}
};
private static final String FEEDS = "feeds";
private static final String PROCESSES = "processes";
private static final String CONFIG = "config";
private final WebResource service;
private final AuthenticatedURL.Token authenticationToken;
/**
* debugMode=false means no debugging. debugMode=true means debugging on.
*/
private boolean debugMode = false;
private final Properties clientProperties;
/**
* Create a Falcon client instance.
*
* @param falconUrl of the server to which client interacts
* @ - If unable to initialize SSL Props
*/
public FalconClient(String falconUrl) {
this(falconUrl, new Properties());
}
/**
* Create a Falcon client instance.
*
* @param falconUrl of the server to which client interacts
* @param properties client properties
* @ - If unable to initialize SSL Props
*/
public FalconClient(String falconUrl, Properties properties) {
try {
String baseUrl = notEmpty(falconUrl, "FalconUrl");
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
this.clientProperties = properties;
SSLContext sslContext = getSslContext();
DefaultClientConfig config = new DefaultClientConfig();
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
new HTTPSProperties(ALL_TRUSTING_HOSTNAME_VERIFIER, sslContext)
);
Client client = Client.create(config);
client.setConnectTimeout(Integer.parseInt(clientProperties.getProperty("falcon.connect.timeout",
"180000")));
client.setReadTimeout(Integer.parseInt(clientProperties.getProperty("falcon.read.timeout", "180000")));
service = client.resource(UriBuilder.fromUri(baseUrl).build());
client.resource(UriBuilder.fromUri(baseUrl).build());
authenticationToken = getToken(baseUrl);
} catch (Exception e) {
LOG.error("Unable to initialize Falcon Client object. Cause : ", e);
throw new FalconCLIException("Unable to initialize Falcon Client object. Cause : " + e.getMessage(), e);
}
}
private static SSLContext getSslContext() throws Exception {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(
null,
new TrustManager[]{TrustManagerUtils.getValidateServerCertificateTrustManager()},
new SecureRandom());
return sslContext;
}
/**
* @return current debug Mode
*/
public boolean getDebugMode() {
return debugMode;
}
/**
* Set debug mode.
*
* @param debugMode : debugMode=false means no debugging. debugMode=true means debugging on
*/
public void setDebugMode(boolean debugMode) {
this.debugMode = debugMode;
}
public static AuthenticatedURL.Token getToken(String baseUrl) {
AuthenticatedURL.Token currentToken = new AuthenticatedURL.Token();
try {
URL url = new URL(baseUrl + AUTH_URL);
// using KerberosAuthenticator which falls back to PsuedoAuthenticator
// instead of passing authentication type from the command line - bad factory
HttpsURLConnection.setDefaultSSLSocketFactory(getSslContext().getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(ALL_TRUSTING_HOSTNAME_VERIFIER);
new AuthenticatedURL(AUTHENTICATOR).openConnection(url, currentToken);
} catch (Exception ex) {
throw new FalconCLIException("Could not authenticate, " + ex.getMessage(), ex);
}
return currentToken;
}
/**
* Methods allowed on Entity Resources.
*/
protected static enum Entities {
VALIDATE("api/entities/validate/", HttpMethod.POST, MediaType.TEXT_XML),
SUBMIT("api/entities/submit/", HttpMethod.POST, MediaType.TEXT_XML),
UPDATE("api/entities/update/", HttpMethod.POST, MediaType.TEXT_XML),
UPDATEDEPENDENTS("api/entities/updateClusterDependents/", HttpMethod.POST, MediaType.TEXT_XML),
SUBMITANDSCHEDULE("api/entities/submitAndSchedule/", HttpMethod.POST, MediaType.TEXT_XML),
SCHEDULE("api/entities/schedule/", HttpMethod.POST, MediaType.TEXT_XML),
SUSPEND("api/entities/suspend/", HttpMethod.POST, MediaType.TEXT_XML),
RESUME("api/entities/resume/", HttpMethod.POST, MediaType.TEXT_XML),
DELETE("api/entities/delete/", HttpMethod.DELETE, MediaType.TEXT_XML),
STATUS("api/entities/status/", HttpMethod.GET, MediaType.TEXT_XML),
DEFINITION("api/entities/definition/", HttpMethod.GET, MediaType.TEXT_XML),
LIST("api/entities/list/", HttpMethod.GET, MediaType.TEXT_XML),
SUMMARY("api/entities/summary", HttpMethod.GET, MediaType.APPLICATION_JSON),
LOOKUP("api/entities/lookup/", HttpMethod.GET, MediaType.APPLICATION_JSON),
DEPENDENCY("api/entities/dependencies/", HttpMethod.GET, MediaType.TEXT_XML),
SLA("api/entities/sla-alert", HttpMethod.GET, MediaType.APPLICATION_JSON),
TOUCH("api/entities/touch", HttpMethod.POST, MediaType.TEXT_XML);
private String path;
private String method;
private String mimeType;
Entities(String path, String method, String mimeType) {
this.path = path;
this.method = method;
this.mimeType = mimeType;
}
}
/**
* Methods allowed on Metadata Discovery Resources.
*/
protected static enum MetadataOperations {
LIST("api/metadata/discovery/", HttpMethod.GET, MediaType.APPLICATION_JSON),
RELATIONS("api/metadata/discovery/", HttpMethod.GET, MediaType.APPLICATION_JSON),
VERTICES("api/metadata/lineage/vertices", HttpMethod.GET, MediaType.APPLICATION_JSON),
EDGES("api/metadata/lineage/edges", HttpMethod.GET, MediaType.APPLICATION_JSON),
LINEAGE("api/metadata/lineage/entities", HttpMethod.GET, MediaType.APPLICATION_JSON);
private String path;
private String method;
private String mimeType;
MetadataOperations(String path, String method, String mimeType) {
this.path = path;
this.method = method;
this.mimeType = mimeType;
}
}
/**
* Methods allowed on Process Instance Resources.
*/
protected static enum Instances {
RUNNING("api/instance/running/", HttpMethod.GET, MediaType.APPLICATION_JSON),
STATUS("api/instance/status/", HttpMethod.GET, MediaType.APPLICATION_JSON),
LIST("api/instance/list", HttpMethod.GET, MediaType.APPLICATION_JSON),
KILL("api/instance/kill/", HttpMethod.POST, MediaType.APPLICATION_JSON),
SUSPEND("api/instance/suspend/", HttpMethod.POST, MediaType.APPLICATION_JSON),
RESUME("api/instance/resume/", HttpMethod.POST, MediaType.APPLICATION_JSON),
RERUN("api/instance/rerun/", HttpMethod.POST, MediaType.APPLICATION_JSON),
LOG("api/instance/logs/", HttpMethod.GET, MediaType.APPLICATION_JSON),
SUMMARY("api/instance/summary/", HttpMethod.GET, MediaType.APPLICATION_JSON),
PARAMS("api/instance/params/", HttpMethod.GET, MediaType.APPLICATION_JSON),
DEPENDENCY("api/instance/dependencies/", HttpMethod.GET, MediaType.APPLICATION_JSON),
TRIAGE("api/instance/triage/", HttpMethod.GET, MediaType.APPLICATION_JSON),
LISTING("api/instance/listing/", HttpMethod.GET, MediaType.APPLICATION_JSON),
SEARCH("api/instance/search/", HttpMethod.GET, MediaType.APPLICATION_JSON);
private String path;
private String method;
private String mimeType;
Instances(String path, String method, String mimeType) {
this.path = path;
this.method = method;
this.mimeType = mimeType;
}
}
protected static enum AdminOperations {
STACK("api/admin/stack", HttpMethod.GET, MediaType.TEXT_PLAIN),
VERSION("api/admin/version", HttpMethod.GET, MediaType.APPLICATION_JSON),
SAFEMODE("api/admin/setSafeMode", HttpMethod.GET, MediaType.APPLICATION_JSON);
private String path;
private String method;
private String mimeType;
AdminOperations(String path, String method, String mimeType) {
this.path = path;
this.method = method;
this.mimeType = mimeType;
}
}
/**
* Methods allowed on Extension Resources.
*/
protected static enum ExtensionOperations {
ENUMERATE("api/extension/enumerate/", HttpMethod.GET, MediaType.TEXT_XML),
DESCRIBE("api/extension/describe/", HttpMethod.GET, MediaType.TEXT_XML),
DEFINITION("api/extension/definition", HttpMethod.GET, MediaType.TEXT_XML),
LIST("api/extension/list", HttpMethod.GET, MediaType.APPLICATION_JSON),
INSTANCES("api/extension/instances", HttpMethod.GET, MediaType.APPLICATION_JSON),
SUBMIT("api/extension/submit", HttpMethod.POST, MediaType.TEXT_XML),
SUBMIT_AND_SCHEDULE("api/extension/submitAndSchedule", HttpMethod.POST, MediaType.TEXT_XML),
UPDATE("api/extension/update", HttpMethod.POST, MediaType.TEXT_XML),
VALIDATE("api/extension/validate", HttpMethod.POST, MediaType.TEXT_XML),
SCHEDULE("api/extension/schedule", HttpMethod.POST, MediaType.TEXT_XML),
SUSPEND("api/extension/suspend", HttpMethod.POST, MediaType.TEXT_XML),
RESUME("api/extension/resume", HttpMethod.POST, MediaType.TEXT_XML),
DELETE("api/extension/delete", HttpMethod.POST, MediaType.TEXT_XML),
UNREGISTER("api/extension/unregister/", HttpMethod.POST, MediaType.TEXT_XML),
DETAIL("api/extension/detail/", HttpMethod.GET, MediaType.TEXT_XML),
JOB_DETAILS("api/extension/extensionJobDetails/", HttpMethod.GET, MediaType.TEXT_XML),
REGISTER("api/extension/register/", HttpMethod.POST, MediaType.TEXT_XML),
ENABLE("api/extension/enable", HttpMethod.POST, MediaType.TEXT_XML),
DISABLE("api/extension/disable", HttpMethod.POST, MediaType.TEXT_XML);
private String path;
private String method;
private String mimeType;
ExtensionOperations(String path, String method, String mimeType) {
this.path = path;
this.method = method;
this.mimeType = mimeType;
}
}
private String notEmpty(String str, String name) {
if (str == null) {
throw new IllegalArgumentException(name + " cannot be null");
}
if (str.length() == 0) {
throw new IllegalArgumentException(name + " cannot be empty");
}
return str;
}
public APIResult schedule(EntityType entityType, String entityName, String colo,
Boolean skipDryRun, String doAsUser, String properties) {
String type = entityType.toString().toLowerCase();
ClientResponse clientResponse = new ResourceBuilder().path(Entities.SCHEDULE.path, type, entityName)
.addQueryParam(COLO, colo).addQueryParam(SKIP_DRYRUN, skipDryRun)
.addQueryParam(PROPERTIES, properties).addQueryParam(DO_AS_OPT, doAsUser).call(Entities.SCHEDULE);
return getResponse(APIResult.class, clientResponse);
}
public APIResult suspend(EntityType entityType, String entityName, String colo, String doAsUser) {
String type = entityType.toString().toLowerCase();
ClientResponse clientResponse = new ResourceBuilder().path(Entities.SUSPEND.path, type, entityName)
.addQueryParam(COLO, colo).addQueryParam(DO_AS_OPT, doAsUser).call(Entities.SUSPEND);
return getResponse(APIResult.class, clientResponse);
}
public APIResult resume(EntityType entityType, String entityName, String colo, String doAsUser) {
String type = entityType.toString().toLowerCase();
ClientResponse clientResponse = new ResourceBuilder().path(Entities.RESUME.path, type, entityName)
.addQueryParam(COLO, colo).addQueryParam(DO_AS_OPT, doAsUser).call(Entities.RESUME);
return getResponse(APIResult.class, clientResponse);
}
public APIResult delete(EntityType entityType, String entityName, String doAsUser) {
String type = entityType.toString().toLowerCase();
ClientResponse clientResponse = new ResourceBuilder().path(Entities.DELETE.path, type, entityName)
.addQueryParam(DO_AS_OPT, doAsUser).call(Entities.DELETE);
return getResponse(APIResult.class, clientResponse);
}
public APIResult validate(String entityType, String filePath, Boolean skipDryRun, String doAsUser) {
InputStream entityStream = getServletInputStream(filePath);
ClientResponse clientResponse = new ResourceBuilder().path(Entities.VALIDATE.path, entityType)
.addQueryParam(SKIP_DRYRUN, skipDryRun).addQueryParam(DO_AS_OPT, doAsUser)
.call(Entities.VALIDATE, entityStream);
return getResponse(APIResult.class, clientResponse);
}
public APIResult submit(String entityType, String filePath, String doAsUser) {
InputStream entityStream = getServletInputStream(filePath);
ClientResponse clientResponse = new ResourceBuilder().path(Entities.SUBMIT.path, entityType)
.addQueryParam(DO_AS_OPT, doAsUser).call(Entities.SUBMIT, entityStream);
return getResponse(APIResult.class, clientResponse);
}
public APIResult update(String entityType, String entityName, String filePath,
Boolean skipDryRun, String doAsUser) {
InputStream entityStream = getServletInputStream(filePath);
Entities operation = Entities.UPDATE;
ClientResponse clientResponse = new ResourceBuilder().path(operation.path, entityType, entityName)
.addQueryParam(SKIP_DRYRUN, skipDryRun).addQueryParam(DO_AS_OPT, doAsUser)
.call(operation, entityStream);
return getResponse(APIResult.class, clientResponse);
}
public APIResult updateClusterDependents(String clusterName, Boolean skipDryRun,
String doAsUser) {
ClientResponse clientResponse = new ResourceBuilder().path(Entities.UPDATEDEPENDENTS.path, clusterName)
.addQueryParam(SKIP_DRYRUN, skipDryRun).addQueryParam(DO_AS_OPT, doAsUser)
.call(Entities.UPDATEDEPENDENTS);
return getResponse(APIResult.class, clientResponse);
}
@Override
public APIResult submitAndSchedule(String entityType, String filePath, Boolean skipDryRun,
String doAsUser, String properties) {
InputStream entityStream = getServletInputStream(filePath);
ClientResponse clientResponse = new ResourceBuilder().path(Entities.SUBMITANDSCHEDULE.path, entityType)
.addQueryParam(SKIP_DRYRUN, skipDryRun).addQueryParam(DO_AS_OPT, doAsUser)
.addQueryParam(PROPERTIES, properties).call(Entities.SUBMITANDSCHEDULE, entityStream);
return getResponse(APIResult.class, clientResponse);
}
public APIResult getStatus(EntityType entityType, String entityName, String colo,
String doAsUser, boolean showScheduler) {
String type = entityType.toString().toLowerCase();
ClientResponse clientResponse = new ResourceBuilder().path(Entities.STATUS.path, type, entityName)
.addQueryParam(COLO, colo).addQueryParam(DO_AS_OPT, doAsUser)
.addQueryParam(SHOW_SCHEDULER, showScheduler).call(Entities.STATUS);
return getResponse(APIResult.class, clientResponse);
}
public Entity getDefinition(String entityType, String entityName, String doAsUser) {
ClientResponse clientResponse = new ResourceBuilder().path(Entities.DEFINITION.path, entityType, entityName)
.call(Entities.DEFINITION);
String entity = getResponseAsString(clientResponse);
return Entity.fromString(EntityType.getEnum(entityType), entity);
}
public EntityList getDependency(String entityType, String entityName, String doAsUser) {
ClientResponse clientResponse = new ResourceBuilder().path(Entities.DEPENDENCY.path, entityType, entityName)
.addQueryParam(DO_AS_OPT, doAsUser).call(Entities.DEPENDENCY);
printClientResponse(clientResponse);
checkIfSuccessful(clientResponse);
EntityList result = clientResponse.getEntity(EntityList.class);
if (result == null) {
return new EntityList();
}
return result;
}
//SUSPEND CHECKSTYLE CHECK ParameterNumberCheck
public SchedulableEntityInstanceResult getFeedSlaMissPendingAlerts(String entityType, String entityName,
String startTime, String endTime, String colo) {
ClientResponse clientResponse = new ResourceBuilder().path(Entities.SLA.path, entityType)
.addQueryParam(START, startTime).addQueryParam(COLO, colo).addQueryParam(END, endTime)
.addQueryParam(ENTITY_NAME, entityName).call(Entities.SLA);
return getResponse(SchedulableEntityInstanceResult.class, clientResponse);
}
public TriageResult triage(String entityType, String entityName, String instanceTime,
String colo) {
ClientResponse clientResponse = new ResourceBuilder().path(Instances.TRIAGE.path, entityType, entityName)
.addQueryParam(START, instanceTime).addQueryParam(COLO, colo).call(Instances.TRIAGE);
return getResponse(TriageResult.class, clientResponse);
}
@Override
public EntityList getEntityList(String entityType, String fields, String nameSubsequence, String tagKeywords,
String filterBy, String filterTags, String orderBy, String sortOrder,
Integer offset, Integer numResults, String doAsUser) {
Entities operation = Entities.LIST;
ClientResponse clientResponse = new ResourceBuilder().path(operation.path, entityType)
.addQueryParam(DO_AS_OPT, doAsUser).addQueryParam(NUM_RESULTS, numResults)
.addQueryParam(OFFSET, offset).addQueryParam(SORT_ORDER, sortOrder)
.addQueryParam(ORDER_BY, orderBy).addQueryParam(FILTER_BY, filterBy)
.addQueryParam(FIELDS, fields).addQueryParam(NAME_SUBSEQUENCE, nameSubsequence)
.addQueryParam(TAG_KEYWORDS, tagKeywords).addQueryParam(FILTER_TAGS, filterTags)
.call(operation);
printClientResponse(clientResponse);
checkIfSuccessful(clientResponse);
EntityList result = clientResponse.getEntity(EntityList.class);
if (result == null || result.getElements() == null) {
return null;
}
return result;
}
@Override
public EntitySummaryResult getEntitySummary(String entityType, String cluster, String start, String end,
String fields, String filterBy, String filterTags,
String orderBy, String sortOrder, Integer offset, Integer numResults,
Integer numInstances, String doAsUser) {
ClientResponse clientResponse = new ResourceBuilder().path(Entities.SUMMARY.path, entityType)
.addQueryParam(CLUSTER, cluster).addQueryParam(START, start).addQueryParam(END, end)
.addQueryParam(SORT_ORDER, sortOrder).addQueryParam(ORDER_BY, orderBy)
.addQueryParam(OFFSET, offset).addQueryParam(NUM_RESULTS, numResults)
.addQueryParam(DO_AS_OPT, doAsUser).addQueryParam(FILTER_BY, filterBy)
.addQueryParam(NUM_INSTANCES, numInstances).addQueryParam(FIELDS, fields)
.addQueryParam(FILTER_TAGS, filterTags).call(Entities.SUMMARY);
return getResponse(EntitySummaryResult.class, clientResponse);
}
@Override
public APIResult touch(String entityType, String entityName, String colo,
Boolean skipDryRun, String doAsUser) {
Entities operation = Entities.TOUCH;
ClientResponse clientResponse = new ResourceBuilder().path(operation.path, entityType, entityName)
.addQueryParam(COLO, colo).addQueryParam(SKIP_DRYRUN, skipDryRun)
.addQueryParam(DO_AS_OPT, doAsUser).call(operation);
return getResponse(APIResult.class, clientResponse);
}
public InstancesResult getRunningInstances(String type, String entity, String colo, List<LifeCycle> lifeCycles,
String filterBy, String orderBy, String sortOrder,
Integer offset, Integer numResults, String doAsUser) {
ClientResponse clientResponse = new ResourceBuilder().path(Instances.RUNNING.path, type, entity)
.addQueryParam(FILTER_BY, filterBy).addQueryParam(ORDER_BY, orderBy)
.addQueryParam(SORT_ORDER, sortOrder).addQueryParam(OFFSET, offset)
.addQueryParam(NUM_RESULTS, numResults).addQueryParam(COLO, colo)
.addQueryParam(LIFECYCLE, lifeCycles, type).addQueryParam(USER, doAsUser).call(Instances.RUNNING);
return getResponse(InstancesResult.class, clientResponse);
}
@Override
public InstancesResult getStatusOfInstances(String type, String entity, String start, String end, String colo,
List<LifeCycle> lifeCycles, String filterBy, String orderBy,
String sortOrder, Integer offset, Integer numResults,
String doAsUser, Boolean allAttempts) {
ClientResponse clientResponse = new ResourceBuilder().path(Instances.STATUS.path, type, entity)
.addQueryParam(START, start).addQueryParam(END, end).addQueryParam(COLO, colo)
.addQueryParam(LIFECYCLE, lifeCycles, type).addQueryParam(FILTER_BY, filterBy)
.addQueryParam(ORDER_BY, orderBy).addQueryParam(SORT_ORDER, sortOrder)
.addQueryParam(OFFSET, offset).addQueryParam(NUM_RESULTS, numResults)
.addQueryParam(ALL_ATTEMPTS, allAttempts).addQueryParam(USER, doAsUser).call(Instances.STATUS);
return getResponse(InstancesResult.class, clientResponse);
}
public InstancesSummaryResult getSummaryOfInstances(String type, String entity,
String start, String end,
String colo, List<LifeCycle> lifeCycles,
String filterBy, String orderBy, String sortOrder,
String doAsUser) {
ClientResponse clientResponse = new ResourceBuilder().path(Instances.SUMMARY.path, type, entity)
.addQueryParam(START, start).addQueryParam(END, end).addQueryParam(COLO, colo)
.addQueryParam(LIFECYCLE, lifeCycles, type).addQueryParam(USER, doAsUser).call(Instances.SUMMARY);
return getResponse(InstancesSummaryResult.class, clientResponse);
}
public FeedInstanceResult getFeedListing(String type, String entity, String start,
String end, String colo, String doAsUser) {
ClientResponse clientResponse = new ResourceBuilder().path(Instances.KILL.path, type, entity)
.addQueryParam(START, start).addQueryParam(END, end).addQueryParam(COLO, colo)
.addQueryParam(USER, doAsUser).call(Instances.LISTING);
return getResponse(FeedInstanceResult.class, clientResponse);
}
public InstancesResult searchInstances(String type, String nameSubsequence, String tagKeywords,
String start, String end, String status, String orderBy,
Integer offset, Integer numResults) {
ClientResponse clientResponse = new ResourceBuilder().path(Instances.SEARCH.path)
.addQueryParam(ENTITY_TYPE, type)
.addQueryParam(NAME_SUBSEQUENCE, nameSubsequence)
.addQueryParam(TAG_KEYWORDS, tagKeywords)
.addQueryParam(START, start)
.addQueryParam(END, end)
.addQueryParam(INSTANCE_STATUS, status)
.addQueryParam(ORDER_BY, orderBy)
.addQueryParam(OFFSET, offset)
.addQueryParam(NUM_RESULTS, numResults)
.call(Instances.SEARCH);
return getResponse(InstancesResult.class, clientResponse);
}
public InstancesResult killInstances(String type, String entity, String start,
String end, String colo, String clusters,
String sourceClusters, List<LifeCycle> lifeCycles,
String doAsUser) throws UnsupportedEncodingException {
InputStream props = getServletInputStream(clusters, sourceClusters, null);
ClientResponse clientResponse = new ResourceBuilder().path(Instances.KILL.path, type, entity)
.addQueryParam(START, start).addQueryParam(END, end).addQueryParam(COLO, colo)
.addQueryParam(LIFECYCLE, lifeCycles, type).addQueryParam(USER, doAsUser).call(Instances.KILL, props);
return getResponse(InstancesResult.class, clientResponse);
}
public InstancesResult suspendInstances(String type, String entity, String start, String end, String colo,
String clusters, String sourceClusters, List<LifeCycle> lifeCycles,
String doAsUser) throws UnsupportedEncodingException {
ClientResponse clientResponse = new ResourceBuilder().path(Instances.SUSPEND.path, type, entity)
.addQueryParam(START, start).addQueryParam(END, end).addQueryParam(COLO, colo)
.addQueryParam(LIFECYCLE, lifeCycles, type).addQueryParam(USER, doAsUser).call(Instances.SUSPEND);
return getResponse(InstancesResult.class, clientResponse);
}
public InstancesResult resumeInstances(String type, String entity, String start, String end, String colo,
String clusters, String sourceClusters, List<LifeCycle> lifeCycles,
String doAsUser) throws UnsupportedEncodingException {
ClientResponse clientResponse = new ResourceBuilder().path(Instances.RESUME.path, type, entity)
.addQueryParam(START, start).addQueryParam(END, end).addQueryParam(COLO, colo)
.addQueryParam(LIFECYCLE, lifeCycles, type).addQueryParam(USER, doAsUser).call(Instances.RESUME);
return getResponse(InstancesResult.class, clientResponse);
}
public InstancesResult rerunInstances(String type, String entity, String start,
String end, String filePath, String colo,
String clusters, String sourceClusters, List<LifeCycle> lifeCycles,
Boolean isForced, String doAsUser) throws IOException {
StringBuilder buffer = new StringBuilder();
if (filePath != null) {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(filePath));
String str;
while ((str = in.readLine()) != null) {
buffer.append(str).append("\n");
}
} finally {
IOUtils.closeQuietly(in);
}
}
String temp = (buffer.length() == 0) ? null : buffer.toString();
InputStream props = getServletInputStream(clusters, sourceClusters, temp);
ClientResponse clientResponse = new ResourceBuilder().path(Instances.RERUN.path, type, entity)
.addQueryParam(START, start).addQueryParam(END, end).addQueryParam(COLO, colo)
.addQueryParam(LIFECYCLE, lifeCycles, type).addQueryParam(FORCE, isForced)
.addQueryParam(USER, doAsUser).call(Instances.RERUN, props);
return getResponse(InstancesResult.class, clientResponse);
}
public InstancesResult getLogsOfInstances(String type, String entity, String start,
String end, String colo, String runId,
List<LifeCycle> lifeCycles, String filterBy,
String orderBy, String sortOrder, Integer offset,
Integer numResults, String doAsUser) {
ClientResponse clientResponse = new ResourceBuilder().path(Instances.LOG.path, type, entity)
.addQueryParam(START, start).addQueryParam(END, end).addQueryParam(COLO, colo)
.addQueryParam(RUN_ID, runId).addQueryParam(LIFECYCLE, lifeCycles, type)
.addQueryParam(FILTER_BY, filterBy).addQueryParam(ORDER_BY, orderBy)
.addQueryParam(SORT_ORDER, sortOrder).addQueryParam(OFFSET, offset)
.addQueryParam(NUM_RESULTS, numResults).addQueryParam(USER, doAsUser).call(Instances.LOG);
return getResponse(InstancesResult.class, clientResponse);
}
public InstancesResult getParamsOfInstance(String type, String entity,
String start, String colo,
List<LifeCycle> lifeCycles,
String doAsUser)
throws UnsupportedEncodingException {
if (!DateValidator.validate(start)) {
throw new FalconCLIException("Start date is mandatory and should be"
+ " a valid date in YYYY-MM-DDTHH:MMZ format.");
}
ClientResponse clientResponse = new ResourceBuilder().path(Instances.PARAMS.path, type, entity)
.addQueryParam(START, start).addQueryParam(LIFECYCLE, lifeCycles, type)
.addQueryParam(USER, doAsUser).call(Instances.PARAMS);
return getResponse(InstancesResult.class, clientResponse);
}
public String getThreadDump(String doAsUser) {
return sendAdminRequest(AdminOperations.STACK, doAsUser);
}
@Override
public String getVersion(String doAsUser) {
return sendAdminRequest(AdminOperations.VERSION, doAsUser);
}
public int getStatus(String doAsUser) {
AdminOperations job = AdminOperations.VERSION;
ClientResponse clientResponse = new ResourceBuilder().path(job.path).addQueryParam(DO_AS_OPT, doAsUser)
.call(job);
printClientResponse(clientResponse);
return clientResponse.getStatus();
}
public ClientResponse setSafemode(String safemode, String doAsUser) {
AdminOperations job = AdminOperations.SAFEMODE;
ClientResponse clientResponse = new ResourceBuilder().path(job.path).path(safemode)
.addQueryParam(DO_AS_OPT, doAsUser).call(job);
printClientResponse(clientResponse);
return clientResponse;
}
public String getDimensionList(String dimensionType, String cluster, String doAsUser) {
return sendMetadataDiscoveryRequest(MetadataOperations.LIST, dimensionType, null, cluster, doAsUser);
}
public String getReplicationMetricsDimensionList(String schedEntityType, String schedEntityName,
Integer numResults, String doAsUser) {
return sendRequestForReplicationMetrics(MetadataOperations.LIST,
schedEntityType, schedEntityName, numResults, doAsUser);
}
public LineageGraphResult getEntityLineageGraph(String pipelineName, String doAsUser) {
MetadataOperations operation = MetadataOperations.LINEAGE;
ClientResponse clientResponse = new ResourceBuilder().path(operation.path).addQueryParam(DO_AS_OPT, doAsUser)
.addQueryParam(FalconCLIConstants.PIPELINE_OPT, pipelineName).call(operation);
printClientResponse(clientResponse);
checkIfSuccessful(clientResponse);
return clientResponse.getEntity(LineageGraphResult.class);
}
public String getDimensionRelations(String dimensionType, String dimensionName,
String doAsUser) {
return sendMetadataDiscoveryRequest(MetadataOperations.RELATIONS, dimensionType, dimensionName, null, doAsUser);
}
private <T> T getResponse(Class<T> clazz, ClientResponse clientResponse) {
printClientResponse(clientResponse);
checkIfSuccessful(clientResponse);
return clientResponse.getEntity(clazz);
}
private String getResponseAsString(ClientResponse clientResponse) {
printClientResponse(clientResponse);
checkIfSuccessful(clientResponse);
return clientResponse.getEntity(String.class);
}
private class ResourceBuilder {
WebResource resource;
private ResourceBuilder path(String... paths) {
for (String path : paths) {
if (resource == null) {
resource = service.path(path);
} else {
resource = resource.path(path);
}
}
return this;
}
private ResourceBuilder addQueryParam(String paramName, Integer value) {
if (value != null) {
resource = resource.queryParam(paramName, value.toString());
}
return this;
}
private ResourceBuilder addQueryParam(String paramName, Boolean paramValue) {
if (paramValue != null) {
resource = resource.queryParam(paramName, String.valueOf(paramValue));
}
return this;
}
private ResourceBuilder addQueryParam(String paramName, String paramValue) {
if (StringUtils.isNotBlank(paramValue)) {
resource = resource.queryParam(paramName, paramValue);
}
return this;
}
private ResourceBuilder addQueryParam(String paramName, List<LifeCycle> lifeCycles,
String type) {
if (lifeCycles != null) {
checkLifeCycleOption(lifeCycles, type);
for (LifeCycle lifeCycle : lifeCycles) {
resource = resource.queryParam(paramName, lifeCycle.toString());
}
}
return this;
}
private ClientResponse call(Entities entities) {
return resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(entities.mimeType).type(MediaType.TEXT_XML)
.method(entities.method, ClientResponse.class);
}
private ClientResponse call(AdminOperations operation) {
return resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(operation.mimeType).type(MediaType.TEXT_XML)
.method(operation.method, ClientResponse.class);
}
private ClientResponse call(MetadataOperations metadataOperations) {
return resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(metadataOperations.mimeType).type(MediaType.TEXT_XML)
.method(metadataOperations.method, ClientResponse.class);
}
private ClientResponse call(Instances operation) {
return resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(operation.mimeType).type(MediaType.TEXT_XML)
.method(operation.method, ClientResponse.class);
}
private ClientResponse call(ExtensionOperations operation) {
return resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(operation.mimeType).type(MediaType.TEXT_XML)
.method(operation.method, ClientResponse.class);
}
private ClientResponse call(Entities operation, InputStream entityStream) {
return resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(operation.mimeType).type(MediaType.TEXT_XML)
.method(operation.method, ClientResponse.class, entityStream);
}
private ClientResponse call(Instances operation, InputStream entityStream) {
return resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(operation.mimeType).type(MediaType.TEXT_XML)
.method(operation.method, ClientResponse.class, entityStream);
}
private ClientResponse call(ExtensionOperations operation, InputStream entityStream) {
return resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(operation.mimeType).type(MediaType.TEXT_XML)
.method(operation.method, ClientResponse.class, entityStream);
}
private ClientResponse call(ExtensionOperations submit, FormDataMultiPart formDataMultiPart) {
return resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(submit.mimeType).type(MediaType.MULTIPART_FORM_DATA)
.method(submit.method, ClientResponse.class, formDataMultiPart);
}
}
public FeedLookupResult reverseLookUp(String type, String path, String doAsUser) {
Entities api = Entities.LOOKUP;
ClientResponse response = new ResourceBuilder().path(api.path, type).addQueryParam(DO_AS_OPT, doAsUser)
.addQueryParam(PATH, path).call(api);
return getResponse(FeedLookupResult.class, response);
}
public FeedInstanceResult getFeedInstanceListing(String type, String entity, String start, String end, String colo
, String doAsUser) {
checkType(type);
Instances api = Instances.LISTING;
ClientResponse clientResponse = new ResourceBuilder().path(api.path, type, entity)
.addQueryParam(COLO, colo).addQueryParam(DO_AS_OPT, doAsUser).addQueryParam(START, start)
.addQueryParam(END, end).call(api);
return getResponse(FeedInstanceResult.class, clientResponse);
}
public InstanceDependencyResult getInstanceDependencies(String entityType, String entityName, String instanceTime,
String colo) {
checkType(entityType);
Instances api = Instances.DEPENDENCY;
ClientResponse clientResponse = new ResourceBuilder().path(api.path, entityType, entityName)
.addQueryParam(COLO, colo).addQueryParam(INSTANCE_TIME, instanceTime).call(api);
return getResponse(InstanceDependencyResult.class, clientResponse);
}
//RESUME CHECKSTYLE CHECK VisibilityModifierCheck
private void checkLifeCycleOption(List<LifeCycle> lifeCycles, String type) {
if (lifeCycles != null && !lifeCycles.isEmpty()) {
EntityType entityType = EntityType.getEnum(type);
for (LifeCycle lifeCycle : lifeCycles) {
if (entityType != lifeCycle.getTag().getType()) {
throw new FalconCLIException("Incorrect lifecycle: " + lifeCycle + "for given type: " + type);
}
}
}
}
private void checkType(String type) {
if (type == null || type.isEmpty()) {
throw new FalconCLIException("entity type is empty");
} else {
EntityType entityType = EntityType.getEnum(type);
if (entityType == EntityType.CLUSTER) {
throw new FalconCLIException(
"Instance management functions don't apply to Cluster entities");
}
}
}
private String sendAdminRequest(AdminOperations job, String doAsUser) {
ClientResponse clientResponse = new ResourceBuilder().path(job.path).addQueryParam(DO_AS_OPT, doAsUser)
.call(job);
return getResponseAsString(clientResponse);
}
private String sendRequestForReplicationMetrics(final MetadataOperations operation, final String schedEntityType,
final String schedEntityName, Integer numResults,
final String doAsUser) {
WebResource resource = service.path(operation.path)
.path(schedEntityName)
.path(RelationshipType.REPLICATION_METRICS.getName())
.path(FalconCLIConstants.LIST_OPT);
if (StringUtils.isNotEmpty(schedEntityName)) {
resource = resource.queryParam(FalconCLIConstants.TYPE_OPT, schedEntityType);
}
if (numResults != null) {
resource = resource.queryParam(FalconCLIConstants.NUM_RESULTS_OPT, numResults.toString());
}
if (StringUtils.isNotEmpty(doAsUser)) {
resource = resource.queryParam(FalconCLIConstants.DO_AS_OPT, doAsUser);
}
ClientResponse clientResponse = resource
.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(operation.mimeType).type(operation.mimeType)
.method(operation.method, ClientResponse.class);
printClientResponse(clientResponse);
checkIfSuccessful(clientResponse);
return clientResponse.getEntity(String.class);
}
private String sendMetadataDiscoveryRequest(final MetadataOperations operation,
final String dimensionType,
final String dimensionName,
final String cluster,
final String doAsUser) {
WebResource resource;
switch (operation) {
case LIST:
resource = service.path(operation.path)
.path(dimensionType)
.path(FalconCLIConstants.LIST_OPT);
break;
case RELATIONS:
resource = service.path(operation.path)
.path(dimensionType)
.path(dimensionName)
.path(FalconCLIConstants.RELATIONS_OPT);
break;
default:
throw new FalconCLIException("Invalid Metadata client Operation " + operation.toString());
}
if (!StringUtils.isEmpty(cluster)) {
resource = resource.queryParam(FalconCLIConstants.CLUSTER_OPT, cluster);
}
if (StringUtils.isNotEmpty(doAsUser)) {
resource = resource.queryParam(FalconCLIConstants.DO_AS_OPT, doAsUser);
}
ClientResponse clientResponse = resource
.header("Cookie", AUTH_COOKIE_EQ + authenticationToken)
.accept(operation.mimeType).type(operation.mimeType)
.method(operation.method, ClientResponse.class);
printClientResponse(clientResponse);
checkIfSuccessful(clientResponse);
return clientResponse.getEntity(String.class);
}