-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
HostResourceProvider.java
1212 lines (1038 loc) · 50.8 KB
/
HostResourceProvider.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.ambari.server.controller.internal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.ClusterNotFoundException;
import org.apache.ambari.server.DuplicateResourceException;
import org.apache.ambari.server.HostNotFoundException;
import org.apache.ambari.server.ObjectNotFoundException;
import org.apache.ambari.server.ParentObjectNotFoundException;
import org.apache.ambari.server.agent.RecoveryConfigHelper;
import org.apache.ambari.server.agent.stomp.HostLevelParamsHolder;
import org.apache.ambari.server.agent.stomp.TopologyHolder;
import org.apache.ambari.server.agent.stomp.dto.HostLevelParamsCluster;
import org.apache.ambari.server.agent.stomp.dto.TopologyCluster;
import org.apache.ambari.server.agent.stomp.dto.TopologyHost;
import org.apache.ambari.server.api.services.AmbariMetaInfo;
import org.apache.ambari.server.controller.AmbariManagementController;
import org.apache.ambari.server.controller.ConfigurationRequest;
import org.apache.ambari.server.controller.HostRequest;
import org.apache.ambari.server.controller.HostResponse;
import org.apache.ambari.server.controller.MaintenanceStateHelper;
import org.apache.ambari.server.controller.RequestStatusResponse;
import org.apache.ambari.server.controller.ServiceComponentHostRequest;
import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
import org.apache.ambari.server.controller.spi.NoSuchResourceException;
import org.apache.ambari.server.controller.spi.Predicate;
import org.apache.ambari.server.controller.spi.Request;
import org.apache.ambari.server.controller.spi.RequestStatus;
import org.apache.ambari.server.controller.spi.Resource;
import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
import org.apache.ambari.server.controller.spi.SystemException;
import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
import org.apache.ambari.server.controller.utilities.PropertyHelper;
import org.apache.ambari.server.events.HostLevelParamsUpdateEvent;
import org.apache.ambari.server.events.TopologyUpdateEvent;
import org.apache.ambari.server.events.UpdateEventType;
import org.apache.ambari.server.security.authorization.AuthorizationException;
import org.apache.ambari.server.security.authorization.AuthorizationHelper;
import org.apache.ambari.server.security.authorization.ResourceType;
import org.apache.ambari.server.security.authorization.RoleAuthorization;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
import org.apache.ambari.server.state.Config;
import org.apache.ambari.server.state.DesiredConfig;
import org.apache.ambari.server.state.Host;
import org.apache.ambari.server.state.MaintenanceState;
import org.apache.ambari.server.state.ServiceComponentHost;
import org.apache.ambari.server.state.stack.OsFamily;
import org.apache.ambari.server.topology.ClusterTopology;
import org.apache.ambari.server.topology.InvalidTopologyException;
import org.apache.ambari.server.topology.InvalidTopologyTemplateException;
import org.apache.ambari.server.topology.LogicalRequest;
import org.apache.ambari.server.topology.TopologyManager;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import com.google.inject.persist.Transactional;
/**
* Resource provider for host resources.
*/
public class HostResourceProvider extends AbstractControllerResourceProvider {
private static final Logger LOG = LoggerFactory.getLogger(HostResourceProvider.class);
// ----- Property ID constants ---------------------------------------------
// Hosts
public static final String RESPONSE_KEY = "Hosts";
public static final String ALL_PROPERTIES = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + "*";
public static final String CLUSTER_NAME_PROPERTY_ID = "cluster_name";
public static final String CPU_COUNT_PROPERTY_ID = "cpu_count";
public static final String DESIRED_CONFIGS_PROPERTY_ID = "desired_configs";
public static final String DISK_INFO_PROPERTY_ID = "disk_info";
public static final String HOST_HEALTH_REPORT_PROPERTY_ID = "host_health_report";
public static final String HOST_NAME_PROPERTY_ID = "host_name";
public static final String HOST_STATUS_PROPERTY_ID = "host_status";
public static final String IP_PROPERTY_ID = "ip";
public static final String LAST_AGENT_ENV_PROPERTY_ID = "last_agent_env";
public static final String LAST_HEARTBEAT_TIME_PROPERTY_ID = "last_heartbeat_time";
public static final String LAST_REGISTRATION_TIME_PROPERTY_ID = "last_registration_time";
public static final String MAINTENANCE_STATE_PROPERTY_ID = "maintenance_state";
public static final String OS_ARCH_PROPERTY_ID = "os_arch";
public static final String OS_FAMILY_PROPERTY_ID = "os_family";
public static final String OS_TYPE_PROPERTY_ID = "os_type";
public static final String PHYSICAL_CPU_COUNT_PROPERTY_ID = "ph_cpu_count";
public static final String PUBLIC_NAME_PROPERTY_ID = "public_host_name";
public static final String RACK_INFO_PROPERTY_ID = "rack_info";
public static final String RECOVERY_REPORT_PROPERTY_ID = "recovery_report";
public static final String RECOVERY_SUMMARY_PROPERTY_ID = "recovery_summary";
public static final String STATE_PROPERTY_ID = "host_state";
public static final String TOTAL_MEM_PROPERTY_ID = "total_mem";
public static final String ATTRIBUTES_PROPERTY_ID = "attributes";
public static final String SUMMARY_PROPERTY_ID = "summary";
public static final String HOST_CLUSTER_NAME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + CLUSTER_NAME_PROPERTY_ID;
public static final String HOST_CPU_COUNT_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + CPU_COUNT_PROPERTY_ID;
public static final String HOST_DESIRED_CONFIGS_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + DESIRED_CONFIGS_PROPERTY_ID;
public static final String HOST_DISK_INFO_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + DISK_INFO_PROPERTY_ID;
public static final String HOST_HOST_HEALTH_REPORT_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + HOST_HEALTH_REPORT_PROPERTY_ID;
public static final String HOST_HOST_STATUS_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + HOST_STATUS_PROPERTY_ID;
public static final String HOST_IP_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + IP_PROPERTY_ID;
public static final String HOST_LAST_AGENT_ENV_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + LAST_AGENT_ENV_PROPERTY_ID;
public static final String HOST_LAST_HEARTBEAT_TIME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + LAST_HEARTBEAT_TIME_PROPERTY_ID;
public static final String HOST_LAST_REGISTRATION_TIME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + LAST_REGISTRATION_TIME_PROPERTY_ID;
public static final String HOST_MAINTENANCE_STATE_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + MAINTENANCE_STATE_PROPERTY_ID;
public static final String HOST_HOST_NAME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + HOST_NAME_PROPERTY_ID;
public static final String HOST_OS_ARCH_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + OS_ARCH_PROPERTY_ID;
public static final String HOST_OS_FAMILY_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + OS_FAMILY_PROPERTY_ID;
public static final String HOST_OS_TYPE_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + OS_TYPE_PROPERTY_ID;
public static final String HOST_PHYSICAL_CPU_COUNT_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + PHYSICAL_CPU_COUNT_PROPERTY_ID;
public static final String HOST_PUBLIC_NAME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + PUBLIC_NAME_PROPERTY_ID;
public static final String HOST_RACK_INFO_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + RACK_INFO_PROPERTY_ID;
public static final String HOST_RECOVERY_REPORT_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + RECOVERY_REPORT_PROPERTY_ID;
public static final String HOST_RECOVERY_SUMMARY_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + RECOVERY_SUMMARY_PROPERTY_ID;
public static final String HOST_STATE_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + STATE_PROPERTY_ID;
public static final String HOST_TOTAL_MEM_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + TOTAL_MEM_PROPERTY_ID;
public static final String HOST_ATTRIBUTES_PROPERTY_ID = PropertyHelper.getPropertyId(RESPONSE_KEY, ATTRIBUTES_PROPERTY_ID);
public static final String BLUEPRINT_PROPERTY_ID = "blueprint";
public static final String HOST_GROUP_PROPERTY_ID = "host_group";
public static final String HOST_COUNT_PROPERTY_ID = "host_count";
public static final String HOST_PREDICATE_PROPERTY_ID = "host_predicate";
//todo use the same json structure for cluster host addition (cluster template and upscale)
/**
* The key property ids for a Host resource.
*/
public static Map<Resource.Type, String> keyPropertyIds = ImmutableMap.<Resource.Type, String>builder()
.put(Resource.Type.Host, HOST_HOST_NAME_PROPERTY_ID)
.put(Resource.Type.Cluster, HOST_CLUSTER_NAME_PROPERTY_ID)
.build();
/**
* The property ids for a Host resource.
*/
public static Set<String> propertyIds = Sets.newHashSet(
HOST_CLUSTER_NAME_PROPERTY_ID,
HOST_CPU_COUNT_PROPERTY_ID,
HOST_DESIRED_CONFIGS_PROPERTY_ID,
HOST_DISK_INFO_PROPERTY_ID,
HOST_HOST_HEALTH_REPORT_PROPERTY_ID,
HOST_HOST_STATUS_PROPERTY_ID,
HOST_IP_PROPERTY_ID,
HOST_LAST_AGENT_ENV_PROPERTY_ID,
HOST_LAST_HEARTBEAT_TIME_PROPERTY_ID,
HOST_LAST_REGISTRATION_TIME_PROPERTY_ID,
HOST_MAINTENANCE_STATE_PROPERTY_ID,
HOST_HOST_NAME_PROPERTY_ID,
HOST_OS_ARCH_PROPERTY_ID,
HOST_OS_FAMILY_PROPERTY_ID,
HOST_OS_TYPE_PROPERTY_ID,
HOST_PHYSICAL_CPU_COUNT_PROPERTY_ID,
HOST_PUBLIC_NAME_PROPERTY_ID,
HOST_RACK_INFO_PROPERTY_ID,
HOST_RECOVERY_REPORT_PROPERTY_ID,
HOST_RECOVERY_SUMMARY_PROPERTY_ID,
HOST_STATE_PROPERTY_ID,
HOST_TOTAL_MEM_PROPERTY_ID,
HOST_ATTRIBUTES_PROPERTY_ID);
@Inject
private MaintenanceStateHelper maintenanceStateHelper;
@Inject
private OsFamily osFamily;
@Inject
private static TopologyManager topologyManager;
@Inject
private static TopologyHolder topologyHolder;
@Inject
private static HostLevelParamsHolder hostLevelParamsHolder;
@Inject
private static RecoveryConfigHelper recoveryConfigHelper;
@Inject
private AmbariMetaInfo ambariMetaInfo;
// ----- Constructors ----------------------------------------------------
/**
* Create a new resource provider for the given management controller.
*
* @param managementController the management controller
*/
@AssistedInject
HostResourceProvider(@Assisted AmbariManagementController managementController) {
super(Resource.Type.Host, propertyIds, keyPropertyIds, managementController);
Set<RoleAuthorization> authorizationsAddDelete = EnumSet.of(RoleAuthorization.HOST_ADD_DELETE_HOSTS);
setRequiredCreateAuthorizations(authorizationsAddDelete);
setRequiredDeleteAuthorizations(authorizationsAddDelete);
setRequiredGetAuthorizations(RoleAuthorization.AUTHORIZATIONS_VIEW_CLUSTER);
setRequiredUpdateAuthorizations(RoleAuthorization.AUTHORIZATIONS_UPDATE_CLUSTER);
}
// ----- ResourceProvider ------------------------------------------------
@Override
protected RequestStatus createResourcesAuthorized(final Request request)
throws SystemException,
UnsupportedPropertyException,
ResourceAlreadyExistsException,
NoSuchParentResourceException {
RequestStatusResponse createResponse = null;
if (isHostGroupRequest(request)) {
createResponse = submitHostRequests(request);
} else {
createResources(new Command<Void>() {
@Override
public Void invoke() throws AmbariException, AuthorizationException {
createHosts(request);
return null;
}
});
}
notifyCreate(Resource.Type.Host, request);
return getRequestStatus(createResponse);
}
@Override
protected Set<Resource> getResourcesAuthorized(Request request, Predicate predicate)
throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
return getHostResource(request, predicate);
}
private Set<Resource> getHostResource(Request request, Predicate predicate)
throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
final Set<HostRequest> requests = new HashSet<>();
if (predicate == null) {
requests.add(getRequest(null));
}
else {
for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) {
requests.add(getRequest(propertyMap));
}
}
Set<HostResponse> responses = getResources(new Command<Set<HostResponse>>() {
@Override
public Set<HostResponse> invoke() throws AmbariException {
return getHosts(requests);
}
});
Set<String> requestedIds = getRequestPropertyIds(request, predicate);
Set<Resource> resources = new HashSet<>();
for (HostResponse response : responses) {
Resource resource = new ResourceImpl(Resource.Type.Host);
// TODO : properly handle more than one cluster
if (response.getClusterName() != null
&& !response.getClusterName().isEmpty()) {
setResourceProperty(resource, HOST_CLUSTER_NAME_PROPERTY_ID,
response.getClusterName(), requestedIds);
}
setResourceProperty(resource, HOST_HOST_NAME_PROPERTY_ID,
response.getHostname(), requestedIds);
setResourceProperty(resource, HOST_PUBLIC_NAME_PROPERTY_ID,
response.getPublicHostName(), requestedIds);
setResourceProperty(resource, HOST_IP_PROPERTY_ID,
response.getIpv4(), requestedIds);
setResourceProperty(resource, HOST_TOTAL_MEM_PROPERTY_ID,
response.getTotalMemBytes(), requestedIds);
setResourceProperty(resource, HOST_CPU_COUNT_PROPERTY_ID,
response.getCpuCount(), requestedIds);
setResourceProperty(resource, HOST_PHYSICAL_CPU_COUNT_PROPERTY_ID,
response.getPhCpuCount(), requestedIds);
setResourceProperty(resource, HOST_OS_ARCH_PROPERTY_ID,
response.getOsArch(), requestedIds);
setResourceProperty(resource, HOST_OS_TYPE_PROPERTY_ID,
response.getOsType(), requestedIds);
setResourceProperty(resource, HOST_OS_FAMILY_PROPERTY_ID,
response.getOsFamily(), requestedIds);
setResourceProperty(resource, HOST_RACK_INFO_PROPERTY_ID,
response.getRackInfo(), requestedIds);
setResourceProperty(resource, HOST_LAST_HEARTBEAT_TIME_PROPERTY_ID,
response.getLastHeartbeatTime(), requestedIds);
setResourceProperty(resource, HOST_LAST_AGENT_ENV_PROPERTY_ID,
response.getLastAgentEnv(), requestedIds);
setResourceProperty(resource, HOST_LAST_REGISTRATION_TIME_PROPERTY_ID,
response.getLastRegistrationTime(), requestedIds);
setResourceProperty(resource, HOST_HOST_STATUS_PROPERTY_ID,
response.getStatus(),requestedIds);
setResourceProperty(resource, HOST_HOST_HEALTH_REPORT_PROPERTY_ID,
response.getHealthReport(), requestedIds);
setResourceProperty(resource, HOST_RECOVERY_REPORT_PROPERTY_ID,
response.getRecoveryReport(), requestedIds);
setResourceProperty(resource, HOST_RECOVERY_SUMMARY_PROPERTY_ID,
response.getRecoverySummary(), requestedIds);
setResourceProperty(resource, HOST_DISK_INFO_PROPERTY_ID,
response.getDisksInfo(), requestedIds);
setResourceProperty(resource, HOST_STATE_PROPERTY_ID,
response.getHostState(), requestedIds);
setResourceProperty(resource, HOST_DESIRED_CONFIGS_PROPERTY_ID,
response.getDesiredHostConfigs(), requestedIds);
// only when a cluster request
if (null != response.getMaintenanceState()) {
setResourceProperty(resource, HOST_MAINTENANCE_STATE_PROPERTY_ID,
response.getMaintenanceState(), requestedIds);
}
resources.add(resource);
}
return resources;
}
@Override
protected RequestStatus updateResourcesAuthorized(final Request request, Predicate predicate)
throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
final Set<HostRequest> requests = new HashSet<>();
for (Map<String, Object> propertyMap : getPropertyMaps(request.getProperties().iterator().next(), predicate)) {
requests.add(getRequest(propertyMap));
}
modifyResources(new Command<Void>() {
@Override
public Void invoke() throws AmbariException, AuthorizationException {
updateHosts(requests);
return null;
}
});
notifyUpdate(Resource.Type.Host, request, predicate);
return getRequestStatus(null);
}
@Override
protected RequestStatus deleteResourcesAuthorized(final Request request, Predicate predicate)
throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
final Set<HostRequest> requests = new HashSet<>();
Map<String, String> requestInfoProperties = request.getRequestInfoProperties();
for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) {
requests.add(getRequest(propertyMap));
}
DeleteStatusMetaData deleteStatusMetaData = modifyResources(new Command<DeleteStatusMetaData>() {
@Override
public DeleteStatusMetaData invoke() throws AmbariException {
return deleteHosts(requests, request.isDryRunRequest());
}
});
if (!request.isDryRunRequest()) {
notifyDelete(Resource.Type.Host, predicate);
}
return getRequestStatus(null, null, deleteStatusMetaData);
}
@Override
public Set<String> checkPropertyIds(Set<String> propertyIds) {
Set<String> baseUnsupported = super.checkPropertyIds(propertyIds);
baseUnsupported.remove(BLUEPRINT_PROPERTY_ID);
baseUnsupported.remove(HOST_GROUP_PROPERTY_ID);
baseUnsupported.remove(HOST_NAME_PROPERTY_ID);
baseUnsupported.remove(HOST_COUNT_PROPERTY_ID);
baseUnsupported.remove(HOST_PREDICATE_PROPERTY_ID);
baseUnsupported.remove(RACK_INFO_PROPERTY_ID);
return checkConfigPropertyIds(baseUnsupported, "Hosts");
}
// ----- AbstractResourceProvider ------------------------------------------
@Override
protected Set<String> getPKPropertyIds() {
return new HashSet<>(keyPropertyIds.values());
}
// ----- utility methods ---------------------------------------------------
/**
* Determine if a request is a high level "add hosts" call or a simple lower level request
* to add a host resources.
*
* @param request current request
* @return true if this is a high level "add hosts" request;
* false if it is a simple create host resources request
*/
private boolean isHostGroupRequest(Request request) {
boolean isHostGroupRequest = false;
Set<Map<String, Object>> properties = request.getProperties();
if (properties != null && ! properties.isEmpty()) {
//todo: for now, either all or none of the hosts need to specify a hg. Unable to mix.
String hgName = (String) properties.iterator().next().get(HOST_GROUP_PROPERTY_ID);
isHostGroupRequest = hgName != null && ! hgName.isEmpty();
}
return isHostGroupRequest;
}
/**
* Get a host request object from a map of property values.
*
* @param properties the predicate
*
* @return the component request object
*/
private HostRequest getRequest(Map<String, Object> properties) {
if (properties == null) {
return new HostRequest(null, null);
}
HostRequest hostRequest = new HostRequest(
getHostNameFromProperties(properties),
(String) properties.get(HOST_CLUSTER_NAME_PROPERTY_ID)
);
hostRequest.setPublicHostName((String) properties.get(HOST_PUBLIC_NAME_PROPERTY_ID));
String rackInfo = (String) ((null != properties.get(HOST_RACK_INFO_PROPERTY_ID))? properties.get(HOST_RACK_INFO_PROPERTY_ID):
properties.get(RACK_INFO_PROPERTY_ID));
hostRequest.setRackInfo(rackInfo);
hostRequest.setBlueprintName((String) properties.get(BLUEPRINT_PROPERTY_ID));
hostRequest.setHostGroupName((String) properties.get(HOST_GROUP_PROPERTY_ID));
Object o = properties.get(HOST_MAINTENANCE_STATE_PROPERTY_ID);
if (null != o) {
hostRequest.setMaintenanceState(o.toString());
}
List<ConfigurationRequest> cr = getConfigurationRequests("Hosts", properties);
hostRequest.setDesiredConfigs(cr);
return hostRequest;
}
/**
* Accepts a request with registered hosts and if the request contains a cluster name then will map all of the
* hosts onto that cluster.
* @param request Request that must contain registered hosts, and optionally a cluster.
*/
public synchronized void createHosts(Request request)
throws AmbariException, AuthorizationException {
Set<Map<String, Object>> propertySet = request.getProperties();
if (propertySet == null || propertySet.isEmpty()) {
LOG.warn("Received a create host request with no associated property sets");
return;
}
AmbariManagementController controller = getManagementController();
Clusters clusters = controller.getClusters();
Set<String> duplicates = new HashSet<>();
Set<String> unknowns = new HashSet<>();
Set<String> allHosts = new HashSet<>();
Set<HostRequest> hostRequests = new HashSet<>();
for (Map<String, Object> propertyMap : propertySet) {
HostRequest hostRequest = getRequest(propertyMap);
hostRequests.add(hostRequest);
if (! propertyMap.containsKey(HOST_GROUP_PROPERTY_ID)) {
createHostResource(clusters, duplicates, unknowns, allHosts, hostRequest);
}
}
if (!duplicates.isEmpty()) {
StringBuilder names = new StringBuilder();
boolean first = true;
for (String hName : duplicates) {
if (!first) {
names.append(",");
}
first = false;
names.append(hName);
}
throw new IllegalArgumentException("Invalid request contains"
+ " duplicate hostnames"
+ ", hostnames=" + names);
}
if (!unknowns.isEmpty()) {
StringBuilder names = new StringBuilder();
boolean first = true;
for (String hName : unknowns) {
if (!first) {
names.append(",");
}
first = false;
names.append(hName);
}
throw new IllegalArgumentException("Attempted to add unknown hosts to a cluster. " +
"These hosts have not been registered with the server: " + names);
}
Map<String, Set<String>> hostClustersMap = new HashMap<>();
Map<String, Map<String, String>> hostAttributes = new HashMap<>();
Set<String> allClusterSet = new HashSet<>();
TreeMap<String, TopologyCluster> addedTopologies = new TreeMap<>();
List<HostLevelParamsUpdateEvent> hostLevelParamsUpdateEvents = new ArrayList<>();
for (HostRequest hostRequest : hostRequests) {
if (hostRequest.getHostname() != null &&
!hostRequest.getHostname().isEmpty() &&
hostRequest.getClusterName() != null &&
!hostRequest.getClusterName().isEmpty()){
Set<String> clusterSet = new HashSet<>();
clusterSet.add(hostRequest.getClusterName());
allClusterSet.add(hostRequest.getClusterName());
hostClustersMap.put(hostRequest.getHostname(), clusterSet);
Cluster cl = clusters.getCluster(hostRequest.getClusterName());
String clusterId = Long.toString(cl.getClusterId());
if (!addedTopologies.containsKey(clusterId)) {
addedTopologies.put(clusterId, new TopologyCluster());
}
Host addedHost = clusters.getHost(hostRequest.getHostname());
addedTopologies.get(clusterId).addTopologyHost(new TopologyHost(addedHost.getHostId(),
addedHost.getHostName(),
addedHost.getRackInfo(),
addedHost.getIPv4()));
HostLevelParamsUpdateEvent hostLevelParamsUpdateEvent = new HostLevelParamsUpdateEvent(addedHost.getHostId(),
clusterId,
new HostLevelParamsCluster(
getManagementController().retrieveHostRepositories(cl, addedHost),
recoveryConfigHelper.getRecoveryConfig(cl.getClusterName(),
addedHost.getHostName()),
getManagementController().getBlueprintProvisioningStates(cl.getClusterId(), addedHost.getHostId()),
hostLevelParamsHolder.getHostStacksSettings(addedHost)
));
hostLevelParamsUpdateEvents.add(hostLevelParamsUpdateEvent);
}
}
clusters.updateHostWithClusterAndAttributes(hostClustersMap, hostAttributes);
// TODO add rack change to topology update
updateHostRackInfoIfChanged(clusters, hostRequests);
for (HostLevelParamsUpdateEvent hostLevelParamsUpdateEvent : hostLevelParamsUpdateEvents) {
hostLevelParamsHolder.updateData(hostLevelParamsUpdateEvent);
}
TopologyUpdateEvent topologyUpdateEvent =
new TopologyUpdateEvent(addedTopologies, UpdateEventType.UPDATE);
topologyHolder.updateData(topologyUpdateEvent);
}
/**
* Iterates through the provided host request and checks if there is rack info provided.
* If the rack info differs from the rack info of the host than updates it with the value from
* the host request.
* @param clusters
* @param hostRequests
* @throws AmbariException
* @throws AuthorizationException
*/
private void updateHostRackInfoIfChanged(Clusters clusters, Set<HostRequest> hostRequests)
throws AmbariException, AuthorizationException {
HashSet<String> rackChangeAffectedClusters = new HashSet<>();
for (HostRequest hostRequest : hostRequests) {
String clusterName = hostRequest.getClusterName();
if (StringUtils.isNotBlank(clusterName)) {
Cluster cluster = clusters.getCluster(clusterName);
Host host = clusters.getHost(hostRequest.getHostname());
if (updateHostRackInfoIfChanged(cluster, host, hostRequest))
rackChangeAffectedClusters.add(clusterName);
}
}
// TODO rack change topology update
for (String clusterName: rackChangeAffectedClusters) {
getManagementController().registerRackChange(clusterName);
}
}
/**
* If the rack info provided in the request differs from the rack info of the host
* update the rack info of the host with the value from the host request
*
* @param cluster The cluster to check user privileges against. User is required
* to have {@link RoleAuthorization#HOST_ADD_DELETE_HOSTS} rights on the cluster.
* @param host The host of which rack information is to be updated
* @param hostRequest
* @return true is host was updated otherwise false
* @throws AmbariException
* @throws AuthorizationException
*/
private boolean updateHostRackInfoIfChanged(Cluster cluster, Host host, HostRequest hostRequest)
throws AmbariException, AuthorizationException {
Long resourceId = cluster.getResourceId();
String hostRackInfo = host.getRackInfo();
String requestRackInfo = hostRequest.getRackInfo();
boolean rackChange = requestRackInfo != null && !requestRackInfo.equals(hostRackInfo);
if (rackChange) {
if(!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, resourceId, RoleAuthorization.HOST_ADD_DELETE_HOSTS)) {
throw new AuthorizationException("The authenticated user is not authorized to update host rack information");
}
//TODO topology update
host.setRackInfo(requestRackInfo);
}
return rackChange;
}
private void createHostResource(Clusters clusters, Set<String> duplicates,
Set<String> unknowns, Set<String> allHosts,
HostRequest request)
throws AmbariException {
if (request.getHostname() == null
|| request.getHostname().isEmpty()) {
throw new IllegalArgumentException("Invalid arguments, hostname"
+ " cannot be null");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Received a createHost request, hostname={}, request={}", request.getHostname(), request);
}
if (allHosts.contains(request.getHostname())) {
// throw dup error later
duplicates.add(request.getHostname());
return;
}
allHosts.add(request.getHostname());
try {
// ensure host is registered
clusters.getHost(request.getHostname());
}
catch (HostNotFoundException e) {
unknowns.add(request.getHostname());
return;
}
if (request.getClusterName() != null) {
try {
// validate that cluster_name is valid
clusters.getCluster(request.getClusterName());
} catch (ClusterNotFoundException e) {
throw new ParentObjectNotFoundException("Attempted to add a host to a cluster which doesn't exist: "
+ " clusterName=" + request.getClusterName());
}
}
}
public RequestStatusResponse install(final String cluster, final String hostname,
Collection<String> skipInstallForComponents,
Collection<String> dontSkipInstallForComponents, final boolean skipFailure,
boolean useClusterHostInfo)
throws ResourceAlreadyExistsException,
SystemException,
NoSuchParentResourceException,
UnsupportedPropertyException {
return ((HostComponentResourceProvider) getResourceProvider(Resource.Type.HostComponent)).
install(cluster, hostname, skipInstallForComponents, dontSkipInstallForComponents, skipFailure, useClusterHostInfo);
}
public RequestStatusResponse start(final String cluster, final String hostname)
throws ResourceAlreadyExistsException,
SystemException,
NoSuchParentResourceException,
UnsupportedPropertyException {
return ((HostComponentResourceProvider) getResourceProvider(Resource.Type.HostComponent)).
start(cluster, hostname);
}
protected Set<HostResponse> getHosts(Set<HostRequest> requests) throws AmbariException {
Set<HostResponse> response = new HashSet<>();
AmbariManagementController controller = getManagementController();
for (HostRequest request : requests) {
try {
response.addAll(getHosts(controller, request, osFamily));
} catch (HostNotFoundException e) {
if (requests.size() == 1) {
// only throw exception if 1 request.
// there will be > 1 request in case of OR predicate
throw e;
}
}
}
return response;
}
/**
* @param osFamily provides OS to OS family lookup; may be null if OS family is ignored anyway (eg. for liveness check)
*/
protected static Set<HostResponse> getHosts(AmbariManagementController controller, HostRequest request, OsFamily osFamily)
throws AmbariException {
//TODO/FIXME host can only belong to a single cluster so get host directly from Cluster
//TODO/FIXME what is the requirement for filtering on host attributes?
List<Host> hosts;
Set<HostResponse> response = new HashSet<>();
Cluster cluster = null;
Clusters clusters = controller.getClusters();
String clusterName = request.getClusterName();
String hostName = request.getHostname();
if (clusterName != null) {
//validate that cluster exists, throws exception if it doesn't.
try {
cluster = clusters.getCluster(clusterName);
} catch (ObjectNotFoundException e) {
throw new ParentObjectNotFoundException("Parent Cluster resource doesn't exist", e);
}
}
if (hostName == null) {
hosts = clusters.getHosts();
} else {
hosts = new ArrayList<>();
try {
hosts.add(clusters.getHost(request.getHostname()));
} catch (HostNotFoundException e) {
// add cluster name
throw new HostNotFoundException(clusterName, hostName);
}
}
// retrieve the cluster desired configs once instead of per host
Map<String, DesiredConfig> desiredConfigs = null;
if (null != cluster) {
desiredConfigs = cluster.getDesiredConfigs();
}
for (Host h : hosts) {
if (clusterName != null) {
if (clusters.getClustersForHost(h.getHostName()).contains(cluster)) {
HostResponse r = h.convertToResponse();
r.setClusterName(clusterName);
r.setDesiredHostConfigs(h.getDesiredHostConfigs(cluster, desiredConfigs));
r.setMaintenanceState(h.getMaintenanceState(cluster.getClusterId()));
if (osFamily != null) {
String hostOsFamily = osFamily.find(r.getOsType());
if (hostOsFamily == null) {
LOG.error("Can not find host OS family. For OS type = '{}' and host name = '{}'", r.getOsType(), r.getHostname());
}
r.setOsFamily(hostOsFamily);
}
response.add(r);
} else if (hostName != null) {
throw new HostNotFoundException(clusterName, hostName);
}
} else {
HostResponse r = h.convertToResponse();
Set<Cluster> clustersForHost = clusters.getClustersForHost(h.getHostName());
//todo: host can only belong to a single cluster
if (clustersForHost != null && clustersForHost.size() != 0) {
Cluster clusterForHost = clustersForHost.iterator().next();
r.setClusterName(clusterForHost.getClusterName());
r.setDesiredHostConfigs(h.getDesiredHostConfigs(clusterForHost, null));
r.setMaintenanceState(h.getMaintenanceState(clusterForHost.getClusterId()));
}
response.add(r);
}
}
return response;
}
protected synchronized void updateHosts(Set<HostRequest> requests) throws AmbariException, AuthorizationException {
if (requests.isEmpty()) {
LOG.warn("Received an empty requests set");
return;
}
AmbariManagementController controller = getManagementController();
Clusters clusters = controller.getClusters();
for (HostRequest request : requests) {
if (request.getHostname() == null || request.getHostname().isEmpty()) {
throw new IllegalArgumentException("Invalid arguments, hostname should be provided");
}
}
TreeMap<String, TopologyCluster> topologyUpdates = new TreeMap<>();
for (HostRequest request : requests) {
if (LOG.isDebugEnabled()) {
LOG.debug("Received an updateHost request, hostname={}, request={}", request.getHostname(), request);
}
TopologyHost topologyHost = new TopologyHost();
Host host = clusters.getHost(request.getHostname());
String clusterName = request.getClusterName();
Cluster cluster = clusters.getCluster(clusterName);
Long clusterId = cluster.getClusterId();
Long resourceId = cluster.getResourceId();
topologyHost.setHostId(host.getHostId());
try {
// The below method call throws an exception when trying to create a duplicate mapping in the clusterhostmapping
// table. This is done to detect duplicates during host create. In order to be robust, handle these gracefully.
clusters.mapAndPublishHostsToCluster(new HashSet<>(Arrays.asList(request.getHostname())), clusterName);
} catch (DuplicateResourceException e) {
// do nothing
}
boolean rackChange = updateHostRackInfoIfChanged(cluster, host, request);
if (rackChange) {
topologyHost.setRackName(host.getRackInfo());
}
if (null != request.getPublicHostName()) {
if(!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, resourceId, RoleAuthorization.HOST_ADD_DELETE_HOSTS)) {
throw new AuthorizationException("The authenticated user is not authorized to update host attributes");
}
host.setPublicHostName(request.getPublicHostName());
topologyHost.setHostName(request.getPublicHostName());
}
if (null != clusterName && null != request.getMaintenanceState()) {
if(!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, resourceId, RoleAuthorization.HOST_TOGGLE_MAINTENANCE)) {
throw new AuthorizationException("The authenticated user is not authorized to update host maintenance state");
}
MaintenanceState newState = MaintenanceState.valueOf(request.getMaintenanceState());
MaintenanceState oldState = host.getMaintenanceState(clusterId);
if (!newState.equals(oldState)) {
if (newState.equals(MaintenanceState.IMPLIED_FROM_HOST)
|| newState.equals(MaintenanceState.IMPLIED_FROM_SERVICE)) {
throw new IllegalArgumentException("Invalid arguments, can only set " +
"maintenance state to one of " + EnumSet.of(MaintenanceState.OFF, MaintenanceState.ON));
} else {
host.setMaintenanceState(clusterId, newState);
}
}
}
// Create configurations
if (null != clusterName && null != request.getDesiredConfigs()) {
if (clusters.getHostsForCluster(clusterName).containsKey(host.getHostName())) {
for (ConfigurationRequest cr : request.getDesiredConfigs()) {
if (null != cr.getProperties() && cr.getProperties().size() > 0) {
LOG.info(MessageFormat.format("Applying configuration with tag ''{0}'' to host ''{1}'' in cluster ''{2}''",
cr.getVersionTag(),
request.getHostname(),
clusterName));
cr.setClusterName(cluster.getClusterName());
controller.createConfiguration(cr);
}
Config baseConfig = cluster.getConfig(cr.getType(), cr.getVersionTag());
if (null != baseConfig) {
String authName = controller.getAuthName();
DesiredConfig oldConfig = host.getDesiredConfigs(clusterId).get(cr.getType());
if (host.addDesiredConfig(clusterId, cr.isSelected(), authName, baseConfig)) {
Logger logger = LoggerFactory.getLogger("configchange");
logger.info("(configchange) cluster '" + cluster.getClusterName() + "', "
+ "host '" + host.getHostName() + "' "
+ "changed by: '" + authName + "'; "
+ "type='" + baseConfig.getType() + "' "
+ "version='" + baseConfig.getVersion() + "'"
+ "tag='" + baseConfig.getTag() + "'"
+ (null == oldConfig ? "" : ", from='" + oldConfig.getTag() + "'"));
}
}
}
}
}
if (StringUtils.isNotBlank(clusterName) && rackChange) {
// Authorization check for this update was performed before we got to this point.
controller.registerRackChange(clusterName);
}
if (!topologyUpdates.containsKey(clusterId.toString())) {
topologyUpdates.put(clusterId.toString(), new TopologyCluster());
}
topologyUpdates.get(clusterId.toString()).addTopologyHost(topologyHost);
TopologyUpdateEvent topologyUpdateEvent = new TopologyUpdateEvent(topologyUpdates,
UpdateEventType.UPDATE);
topologyHolder.updateData(topologyUpdateEvent);
//todo: if attempt was made to update a property other than those
//todo: that are allowed above, should throw exception
}
}
@Transactional
protected DeleteStatusMetaData deleteHosts(Set<HostRequest> requests, boolean dryRun)
throws AmbariException {
AmbariManagementController controller = getManagementController();
Clusters clusters = controller.getClusters();
DeleteStatusMetaData deleteStatusMetaData = new DeleteStatusMetaData();
List<HostRequest> okToRemove = new ArrayList<>();
for (HostRequest hostRequest : requests) {
String hostName = hostRequest.getHostname();
if (null == hostName) {
continue;
}
try {
validateHostInDeleteFriendlyState(hostRequest, clusters);
okToRemove.add(hostRequest);
} catch (Exception ex) {
deleteStatusMetaData.addException(hostName, ex);
}
}
//If dry run, don't delete. just assume it can be successfully deleted.
if (dryRun) {
for (HostRequest request : okToRemove) {
deleteStatusMetaData.addDeletedKey(request.getHostname());
}
} else {
processDeleteHostRequests(okToRemove, clusters, deleteStatusMetaData);
}
//Do not break behavior for existing clients where delete request contains only 1 host.
//Response for these requests will have empty body with appropriate error code.
//dryRun is a new feature so its ok to unify the behavior
if (!dryRun) {
if (deleteStatusMetaData.getDeletedKeys().size() + deleteStatusMetaData.getExceptionForKeys().size() == 1) {
if (deleteStatusMetaData.getDeletedKeys().size() == 1) {
return null;
}
for (Map.Entry<String, Exception> entry : deleteStatusMetaData.getExceptionForKeys().entrySet()) {
Exception ex = entry.getValue();
if (ex instanceof AmbariException) {
throw (AmbariException) ex;