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+ * <p>
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ * <p>
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 .server .applicationhistoryservice ;
20+
21+ import org .apache .hadoop .conf .Configuration ;
22+ import org .apache .hadoop .yarn .api .records .timeline .TimelineEntity ;
23+ import org .apache .hadoop .yarn .api .records .timeline .TimelinePutResponse ;
24+ import org .apache .hadoop .yarn .api .records .timeline .reader .TimelineEntityReader ;
25+ import org .apache .hadoop .yarn .client .api .TimelineClient ;
26+ import org .apache .hadoop .yarn .conf .YarnConfiguration ;
27+ import org .apache .hadoop .yarn .exceptions .YarnException ;
28+ import org .apache .hadoop .yarn .server .timeline .MemoryTimelineStore ;
29+ import org .apache .hadoop .yarn .server .timeline .TimelineStore ;
30+ import org .apache .hadoop .yarn .webapp .YarnJacksonJaxbJsonProvider ;
31+ import org .codehaus .jettison .json .JSONObject ;
32+ import org .glassfish .jersey .client .ClientConfig ;
33+ import org .glassfish .jersey .jettison .JettisonFeature ;
34+ import org .junit .jupiter .api .AfterAll ;
35+ import org .junit .jupiter .api .BeforeAll ;
36+ import org .junit .jupiter .api .Test ;
37+ import org .slf4j .Logger ;
38+ import org .slf4j .LoggerFactory ;
39+
40+ import javax .ws .rs .client .Client ;
41+ import javax .ws .rs .client .ClientBuilder ;
42+ import javax .ws .rs .client .WebTarget ;
43+ import javax .ws .rs .core .MediaType ;
44+ import javax .ws .rs .core .Response ;
45+ import java .io .IOException ;
46+
47+ import static org .apache .hadoop .yarn .conf .YarnConfiguration .TIMELINE_HTTP_AUTH_PREFIX ;
48+ import static org .junit .jupiter .api .Assertions .*;
49+
50+ public class TestTimelineServerWithTezRequests {
51+ private static final Logger LOG = LoggerFactory .getLogger (TestTimelineServerWithTezRequests .class );
52+
53+ private static final String BASEDIR =
54+ System .getProperty ("test.build.dir" , "target/test-dir" ) + "/"
55+ + TestTimelineServerWithTezRequests .class .getSimpleName ();
56+ private static final String TIMELINE_SERVICE_WEBAPP_ADDRESS = "localhost:8188" ;
57+ private static final String TEZ_ENTITY_TYPE = "TEZ_APPLICATION" ;
58+ private static final String TEZ_ENTITY_ID = "tez_application_1_2" ;
59+ private static ApplicationHistoryServer testTimelineServer ;
60+ private static Configuration conf ;
61+
62+ @ BeforeAll
63+ public static void setup () {
64+ try {
65+ testTimelineServer = new ApplicationHistoryServer ();
66+ conf = new Configuration (false );
67+ conf .setStrings (TIMELINE_HTTP_AUTH_PREFIX + "type" , "simple" );
68+
69+ conf .setBoolean (YarnConfiguration .TIMELINE_SERVICE_ENABLED , true );
70+ conf .setClass (YarnConfiguration .TIMELINE_SERVICE_STORE , MemoryTimelineStore .class , TimelineStore .class );
71+ conf .set (YarnConfiguration .TIMELINE_SERVICE_WEBAPP_ADDRESS , TIMELINE_SERVICE_WEBAPP_ADDRESS );
72+ conf .setInt (YarnConfiguration .TIMELINE_SERVICE_CLIENT_MAX_RETRIES , 1 );
73+
74+ testTimelineServer .init (conf );
75+ testTimelineServer .start ();
76+ } catch (Exception e ) {
77+ LOG .error ("Failed to setup TimelineServer" , e );
78+ fail ("Couldn't setup TimelineServer" );
79+ }
80+ }
81+
82+ @ AfterAll
83+ public static void tearDown () throws Exception {
84+ if (testTimelineServer != null ) {
85+ testTimelineServer .stop ();
86+ }
87+ }
88+
89+ @ Test
90+ void testPutAndGetTimelineEntity () throws Exception {
91+ putEntity ();
92+ getEntity ();
93+ }
94+
95+ private void putEntity () throws IOException , YarnException {
96+ try (TimelineClient client = createTimelineClient ()) {
97+ TimelineEntity entityToStore = new TimelineEntity ();
98+ entityToStore .setEntityType (TEZ_ENTITY_TYPE );
99+ entityToStore .setEntityId (TEZ_ENTITY_ID );
100+ entityToStore .setStartTime (System .currentTimeMillis ());
101+ TimelinePutResponse putResponse = client .putEntities (entityToStore );
102+ if (!putResponse .getErrors ().isEmpty ()) {
103+ LOG .error ("putResponse errors: {}" , putResponse .getErrors ());
104+ }
105+ assertTrue (putResponse .getErrors ().isEmpty (), "There were some errors in the putResponse" );
106+ TimelineEntity entityToRead =
107+ testTimelineServer .getTimelineStore ().getEntity (TEZ_ENTITY_ID , TEZ_ENTITY_TYPE , null );
108+ assertNotNull (entityToRead , "Timeline entity should not be null" );
109+ }
110+ }
111+
112+ private TimelineClient createTimelineClient () {
113+ TimelineClient client = TimelineClient .createTimelineClient ();
114+ client .init (conf );
115+ client .start ();
116+ return client ;
117+ }
118+
119+ private void getEntity () {
120+ String appUrl = "http://" + TIMELINE_SERVICE_WEBAPP_ADDRESS + "/ws/v1/timeline/"
121+ + TEZ_ENTITY_TYPE + "/" + TEZ_ENTITY_ID + "?user.name=foo" ;
122+ LOG .info ("Getting timeline entity for tez application: " + appUrl );
123+
124+ ClientConfig cfg = new ClientConfig ();
125+ cfg .register (new JettisonFeature ()).register (YarnJacksonJaxbJsonProvider .class );
126+ Client client = ClientBuilder .newClient (cfg );
127+ client .register (TimelineEntityReader .class );
128+ WebTarget target = client .target (appUrl );
129+
130+ Response response = target .request (MediaType .APPLICATION_JSON ).get (Response .class );
131+ assertEquals (200 , response .getStatus ());
132+ assertTrue (MediaType .APPLICATION_JSON_TYPE .isCompatible (response .getMediaType ()));
133+
134+ JSONObject entityStr = response .readEntity (JSONObject .class );
135+ LOG .info ("Got entity from ATS: {}" , entityStr );
136+ }
137+ }
0 commit comments