Skip to content

Commit 6996edf

Browse files
committed
YARN-11877. Resolve JAXB IllegalAnnotation issue in TimelineEntity with Jersey 2.x.
1 parent d9c3308 commit 6996edf

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
package org.apache.hadoop.yarn.api.records.timeline.reader;
19+
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import org.apache.hadoop.yarn.api.records.timeline.TimelineEntity;
22+
23+
import javax.ws.rs.Consumes;
24+
import javax.ws.rs.WebApplicationException;
25+
import javax.ws.rs.core.MediaType;
26+
import javax.ws.rs.core.MultivaluedMap;
27+
import javax.ws.rs.ext.MessageBodyReader;
28+
import javax.ws.rs.ext.Provider;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.lang.annotation.Annotation;
32+
import java.lang.reflect.Type;
33+
34+
/**
35+
* We have defined a dedicated Reader for TimelineEntity,
36+
* aimed at adapting to the Jersey2 framework
37+
* to ensure that JSON can be converted into TimelineEntity.
38+
*/
39+
@Provider
40+
@Consumes(MediaType.APPLICATION_JSON)
41+
public class TimelineEntityReader implements MessageBodyReader<TimelineEntity> {
42+
43+
private ObjectMapper objectMapper = new ObjectMapper();
44+
45+
@Override
46+
public boolean isReadable(Class<?> type, Type genericType,
47+
Annotation[] annotations, MediaType mediaType) {
48+
return type == TimelineEntity.class;
49+
}
50+
51+
@Override
52+
public TimelineEntity readFrom(Class<TimelineEntity> type, Type genericType,
53+
Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
54+
InputStream entityStream) throws IOException, WebApplicationException {
55+
return objectMapper.readValue(entityStream, TimelineEntity.class);
56+
}
57+
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/ApplicationHistoryServer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
import org.apache.hadoop.yarn.YarnUncaughtExceptionHandler;
4242
import org.apache.hadoop.yarn.api.ApplicationBaseProtocol;
4343
import org.apache.hadoop.yarn.api.records.timeline.reader.TimelineDomainReader;
44+
import org.apache.hadoop.yarn.api.records.timeline.reader.TimelineEntityReader;
4445
import org.apache.hadoop.yarn.api.records.timeline.reader.TimelinePutResponseReader;
4546
import org.apache.hadoop.yarn.api.records.timeline.reader.TimelineEntitiesReader;
47+
import org.apache.hadoop.yarn.api.records.timeline.writer.TimelineEntityWriter;
4648
import org.apache.hadoop.yarn.api.records.timeline.writer.TimelineEntitiesWriter;
4749
import org.apache.hadoop.yarn.conf.YarnConfiguration;
4850
import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
@@ -363,6 +365,8 @@ protected ResourceConfig configure() {
363365
config.register(AHSWebServices.class);
364366
config.register(TimelineEntitiesWriter.class);
365367
config.register(TimelineEntitiesReader.class);
368+
config.register(TimelineEntityReader.class);
369+
config.register(TimelineEntityWriter.class);
366370
config.register(TimelineDomainReader.class);
367371
config.register(TimelinePutResponseReader.class);
368372
config.register(new JerseyBinder());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)