1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.yarn.client.api.impl;
20 
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.util.List;
24 
25 import org.apache.hadoop.classification.InterfaceAudience.Private;
26 import org.apache.hadoop.classification.InterfaceStability.Unstable;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.ipc.RPC;
29 import org.apache.hadoop.yarn.api.ApplicationHistoryProtocol;
30 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptReportRequest;
31 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptReportResponse;
32 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptsRequest;
33 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptsResponse;
34 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationReportRequest;
35 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationReportResponse;
36 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest;
37 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse;
38 import org.apache.hadoop.yarn.api.protocolrecords.GetContainerReportRequest;
39 import org.apache.hadoop.yarn.api.protocolrecords.GetContainerReportResponse;
40 import org.apache.hadoop.yarn.api.protocolrecords.GetContainersRequest;
41 import org.apache.hadoop.yarn.api.protocolrecords.GetContainersResponse;
42 import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
43 import org.apache.hadoop.yarn.api.records.ApplicationAttemptReport;
44 import org.apache.hadoop.yarn.api.records.ApplicationId;
45 import org.apache.hadoop.yarn.api.records.ApplicationReport;
46 import org.apache.hadoop.yarn.api.records.ContainerId;
47 import org.apache.hadoop.yarn.api.records.ContainerReport;
48 import org.apache.hadoop.yarn.client.AHSProxy;
49 import org.apache.hadoop.yarn.client.api.AHSClient;
50 import org.apache.hadoop.yarn.conf.YarnConfiguration;
51 import org.apache.hadoop.yarn.exceptions.YarnException;
52 import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
53 
54 @Private
55 @Unstable
56 public class AHSClientImpl extends AHSClient {
57 
58   protected ApplicationHistoryProtocol ahsClient;
59   protected InetSocketAddress ahsAddress;
60 
AHSClientImpl()61   public AHSClientImpl() {
62     super(AHSClientImpl.class.getName());
63   }
64 
getAHSAddress(Configuration conf)65   private static InetSocketAddress getAHSAddress(Configuration conf) {
66     return conf.getSocketAddr(YarnConfiguration.TIMELINE_SERVICE_ADDRESS,
67         YarnConfiguration.DEFAULT_TIMELINE_SERVICE_ADDRESS,
68         YarnConfiguration.DEFAULT_TIMELINE_SERVICE_PORT);
69   }
70 
71   @Override
serviceInit(Configuration conf)72   protected void serviceInit(Configuration conf) throws Exception {
73     this.ahsAddress = getAHSAddress(conf);
74     super.serviceInit(conf);
75   }
76 
77   @Override
serviceStart()78   protected void serviceStart() throws Exception {
79     try {
80       ahsClient = AHSProxy.createAHSProxy(getConfig(),
81           ApplicationHistoryProtocol.class, this.ahsAddress);
82     } catch (IOException e) {
83       throw new YarnRuntimeException(e);
84     }
85     super.serviceStart();
86   }
87 
88   @Override
serviceStop()89   protected void serviceStop() throws Exception {
90     if (this.ahsClient != null) {
91       RPC.stopProxy(this.ahsClient);
92     }
93     super.serviceStop();
94   }
95 
96   @Override
getApplicationReport(ApplicationId appId)97   public ApplicationReport getApplicationReport(ApplicationId appId)
98       throws YarnException, IOException {
99     GetApplicationReportRequest request = GetApplicationReportRequest
100         .newInstance(appId);
101     GetApplicationReportResponse response = ahsClient
102         .getApplicationReport(request);
103     return response.getApplicationReport();
104   }
105 
106   @Override
getApplications()107   public List<ApplicationReport> getApplications() throws YarnException,
108       IOException {
109     GetApplicationsRequest request = GetApplicationsRequest.newInstance(null,
110         null);
111     GetApplicationsResponse response = ahsClient.getApplications(request);
112     return response.getApplicationList();
113   }
114 
115   @Override
getApplicationAttemptReport( ApplicationAttemptId applicationAttemptId)116   public ApplicationAttemptReport getApplicationAttemptReport(
117       ApplicationAttemptId applicationAttemptId) throws YarnException,
118       IOException {
119     GetApplicationAttemptReportRequest request = GetApplicationAttemptReportRequest
120         .newInstance(applicationAttemptId);
121     GetApplicationAttemptReportResponse response = ahsClient
122         .getApplicationAttemptReport(request);
123     return response.getApplicationAttemptReport();
124   }
125 
126   @Override
getApplicationAttempts( ApplicationId appId)127   public List<ApplicationAttemptReport> getApplicationAttempts(
128       ApplicationId appId) throws YarnException, IOException {
129     GetApplicationAttemptsRequest request = GetApplicationAttemptsRequest
130         .newInstance(appId);
131     GetApplicationAttemptsResponse response = ahsClient
132         .getApplicationAttempts(request);
133     return response.getApplicationAttemptList();
134   }
135 
136   @Override
getContainerReport(ContainerId containerId)137   public ContainerReport getContainerReport(ContainerId containerId)
138       throws YarnException, IOException {
139     GetContainerReportRequest request = GetContainerReportRequest
140         .newInstance(containerId);
141     GetContainerReportResponse response = ahsClient.getContainerReport(request);
142     return response.getContainerReport();
143   }
144 
145   @Override
getContainers( ApplicationAttemptId applicationAttemptId)146   public List<ContainerReport> getContainers(
147       ApplicationAttemptId applicationAttemptId) throws YarnException,
148       IOException {
149     GetContainersRequest request = GetContainersRequest
150         .newInstance(applicationAttemptId);
151     GetContainersResponse response = ahsClient.getContainers(request);
152     return response.getContainerList();
153   }
154 
155 }
156