diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/HistoryStore.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/HistoryStore.java new file mode 100644 index 0000000..60dc466 --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/HistoryStore.java @@ -0,0 +1,97 @@ +/** + * 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.hadoop.yarn.server.applicationhistoryservice; + +import java.util.List; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; +import org.apache.hadoop.yarn.api.records.ApplicationId; +import org.apache.hadoop.yarn.exceptions.YarnException; +import org.apache.hadoop.yarn.server.applicationhistoryservice.records.ApplicationAttemptHistoryData; +import org.apache.hadoop.yarn.server.applicationhistoryservice.records.ApplicationHistoryData; +import org.apache.hadoop.yarn.server.applicationhistoryservice.records.ContainerHistoryData; + +@InterfaceAudience.Public +@InterfaceStability.Unstable +public interface HistoryStore { + + /** + * This method returns {@link ApplicationHistoryData} for the specified + * {@link ApplicationId}. + * + * @return {@link ApplicationHistoryData} for the ApplicationId. + */ + public ApplicationHistoryData getApplication(ApplicationId appId); + + /** + * This method returns List of {@link ApplicationHistoryData} for all + * applications + * + * @return List {@link ApplicationHistoryData}. + */ + public List getAllApplications(); + + /** + * Application can have multiple application attempts + * {@link ApplicationAttemptHistoryData}. This method returns the all + * {@link ApplicationAttemptHistoryData}s for the Application. + * + * @return all {@link ApplicationAttemptHistoryData}s for the Application. + */ + public List getApplicationAttempts( + ApplicationId appId); + + /** + * This method returns List of {@link ContainerHistoryData} for specified + * {@link ApplicationAttemptId}. + * + * @param {@link ApplicationAttemptId} + * @return {@link ContainerHistoryData} for ApplicationAttemptId + */ + public List getContainers( + ApplicationAttemptId appAttemptId); + + /** + * This method adds {@link ApplicationHistoryData} to the store + * + * @throws {@link YarnException} + */ + public void addApplication(ApplicationHistoryData appData) + throws YarnException; + + /** + * This method adds {@link ApplicationAttemptHistoryData} to the store for the + * specified {@link ApplicationId} + * + * @throws {@link YarnException} + */ + public void addApplcationAttempt(ApplicationId appId, + ApplicationAttemptHistoryData appAttempt) throws YarnException; + + /** + * This method adds {@link ContainerHistoryData} to the store for the + * specified {@link ApplicationAttemptId} + * + * @throws {@link YarnException} + */ + public void addContainer(ApplicationAttemptId appAttemptId, + ContainerHistoryData containerHistoryData) throws YarnException; +} diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/MemoryHistoryStore.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/MemoryHistoryStore.java new file mode 100644 index 0000000..8cc5cdf --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/MemoryHistoryStore.java @@ -0,0 +1,142 @@ +package org.apache.hadoop.yarn.server.applicationhistoryservice; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; +import org.apache.hadoop.yarn.api.records.ApplicationId; +import org.apache.hadoop.yarn.api.records.ContainerId; +import org.apache.hadoop.yarn.exceptions.YarnException; +import org.apache.hadoop.yarn.server.applicationhistoryservice.records.ApplicationAttemptHistoryData; +import org.apache.hadoop.yarn.server.applicationhistoryservice.records.ApplicationHistoryData; +import org.apache.hadoop.yarn.server.applicationhistoryservice.records.ContainerHistoryData; + +public class MemoryHistoryStore implements HistoryStore { + + private static MemoryHistoryStore memStore = null; + private ConcurrentHashMap applicationData = + new ConcurrentHashMap(); + private ConcurrentHashMap> applicationAttemptData = + new ConcurrentHashMap>(); + private ConcurrentHashMap> containerData = + new ConcurrentHashMap>(); + + private MemoryHistoryStore() { + } + + public static MemoryHistoryStore getMemoryStore() { + if (memStore == null) { + memStore = new MemoryHistoryStore(); + } + return memStore; + } + + @Override + public List getAllApplications() { + List listApps = + new LinkedList(); + for (ApplicationId appId : applicationData.keySet()) { + listApps.add(applicationData.get(appId)); + } + return listApps; + } + + @Override + public ApplicationHistoryData getApplication(ApplicationId appId) { + return applicationData.get(appId); + } + + @Override + public List getApplicationAttempts( + ApplicationId appId) { + List listAttempts = null; + ConcurrentHashMap appAttempts = + applicationAttemptData.get(appId); + if (appAttempts != null) { + listAttempts = new LinkedList(); + for (ApplicationAttemptId attemptId : appAttempts.keySet()) { + listAttempts.add(appAttempts.get(attemptId)); + } + } + return listAttempts; + } + + @Override + public List getContainers( + ApplicationAttemptId appAttemptId) { + List listContainers = null; + ConcurrentHashMap containers = + containerData.get(appAttemptId); + if (containers != null) { + listContainers = new LinkedList(); + for (ContainerId containerId : containers.keySet()) { + listContainers.add(containers.get(containerId)); + } + } + return listContainers; + } + + @Override + public synchronized void addApplcationAttempt(ApplicationId appId, + ApplicationAttemptHistoryData appAttempt) throws YarnException { + if (appId != null && appAttempt != null) { + if (applicationAttemptData.containsKey(appId)) { + ConcurrentHashMap appAttemptmap = + applicationAttemptData.get(appId); + ApplicationAttemptHistoryData oldAppAttempt = + appAttemptmap.putIfAbsent(appAttempt.getApplicationAttemptId(), + appAttempt); + if (oldAppAttempt != null) { + throw new YarnException("This application attempt " + + appAttempt.getApplicationAttemptId().toString() + + " already present."); + } + } else { + ConcurrentHashMap appAttemptmap = + new ConcurrentHashMap(); + appAttemptmap.put(appAttempt.getApplicationAttemptId(), appAttempt); + applicationAttemptData.putIfAbsent(appId, appAttemptmap); + } + } + } + + @Override + public synchronized void addApplication(ApplicationHistoryData appData) + throws YarnException { + if (appData != null) { + + ApplicationHistoryData oldData = + applicationData.putIfAbsent(appData.getApplicationId(), appData); + if (oldData != null) { + throw new YarnException("This application " + + appData.getApplicationId().toString() + " already present."); + } + } + } + + @Override + public synchronized void addContainer(ApplicationAttemptId appAttemptId, + ContainerHistoryData containerHistoryData) throws YarnException { + if (appAttemptId != null && containerHistoryData != null) { + if (containerData.containsKey(appAttemptId)) { + ConcurrentHashMap containerMap = + containerData.get(appAttemptId); + ContainerHistoryData oldContainer = + containerMap.putIfAbsent(containerHistoryData.getContainerId(), + containerHistoryData); + if (oldContainer != null) { + throw new YarnException("This Container " + + containerHistoryData.getContainerId().toString() + + " already present."); + } + } else { + ConcurrentHashMap containerMap = + new ConcurrentHashMap(); + containerMap.put(containerHistoryData.getContainerId(), + containerHistoryData); + containerData.putIfAbsent(appAttemptId, containerMap); + } + } + } +}