001/**
002* Licensed to the Apache Software Foundation (ASF) under one
003* or more contributor license agreements.  See the NOTICE file
004* distributed with this work for additional information
005* regarding copyright ownership.  The ASF licenses this file
006* to you under the Apache License, Version 2.0 (the
007* "License"); you may not use this file except in compliance
008* with the License.  You may obtain a copy of the License at
009*
010*     http://www.apache.org/licenses/LICENSE-2.0
011*
012* Unless required by applicable law or agreed to in writing, software
013* distributed under the License is distributed on an "AS IS" BASIS,
014* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015* See the License for the specific language governing permissions and
016* limitations under the License.
017*/
018
019package org.apache.hadoop.yarn.security;
020
021import java.io.DataInput;
022import java.io.DataOutput;
023import java.io.IOException;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.apache.hadoop.classification.InterfaceAudience;
028import org.apache.hadoop.classification.InterfaceAudience.Public;
029import org.apache.hadoop.classification.InterfaceStability.Evolving;
030import org.apache.hadoop.io.Text;
031import org.apache.hadoop.security.UserGroupInformation;
032import org.apache.hadoop.security.token.Token;
033import org.apache.hadoop.security.token.TokenIdentifier;
034import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
035import org.apache.hadoop.yarn.api.records.ApplicationId;
036import org.apache.hadoop.yarn.api.records.ContainerId;
037import org.apache.hadoop.yarn.api.records.LogAggregationContext;
038import org.apache.hadoop.yarn.api.records.Priority;
039import org.apache.hadoop.yarn.api.records.Resource;
040
041/**
042 * TokenIdentifier for a container. Encodes {@link ContainerId},
043 * {@link Resource} needed by the container and the target NMs host-address.
044 * 
045 */
046@Public
047@Evolving
048public class ContainerTokenIdentifier extends TokenIdentifier {
049
050  private static Log LOG = LogFactory.getLog(ContainerTokenIdentifier.class);
051
052  public static final Text KIND = new Text("ContainerToken");
053
054  private ContainerId containerId;
055  private String nmHostAddr;
056  private String appSubmitter;
057  private Resource resource;
058  private long expiryTimeStamp;
059  private int masterKeyId;
060  private long rmIdentifier;
061  private Priority priority;
062  private long creationTime;
063  private LogAggregationContext logAggregationContext;
064
065  public ContainerTokenIdentifier(ContainerId containerID,
066      String hostName, String appSubmitter, Resource r, long expiryTimeStamp,
067      int masterKeyId, long rmIdentifier, Priority priority, long creationTime) {
068    this(containerID, hostName, appSubmitter, r, expiryTimeStamp, masterKeyId,
069        rmIdentifier, priority, creationTime, null);
070  }
071
072  public ContainerTokenIdentifier(ContainerId containerID, String hostName,
073      String appSubmitter, Resource r, long expiryTimeStamp, int masterKeyId,
074      long rmIdentifier, Priority priority, long creationTime,
075      LogAggregationContext logAggregationContext) {
076    this.containerId = containerID;
077    this.nmHostAddr = hostName;
078    this.appSubmitter = appSubmitter;
079    this.resource = r;
080    this.expiryTimeStamp = expiryTimeStamp;
081    this.masterKeyId = masterKeyId;
082    this.rmIdentifier = rmIdentifier;
083    this.priority = priority;
084    this.creationTime = creationTime;
085    this.logAggregationContext = logAggregationContext;
086  }
087
088  /**
089   * Default constructor needed by RPC layer/SecretManager.
090   */
091  public ContainerTokenIdentifier() {
092  }
093
094  public ContainerId getContainerID() {
095    return this.containerId;
096  }
097
098  public String getApplicationSubmitter() {
099    return this.appSubmitter;
100  }
101
102  public String getNmHostAddress() {
103    return this.nmHostAddr;
104  }
105
106  public Resource getResource() {
107    return this.resource;
108  }
109
110  public long getExpiryTimeStamp() {
111    return this.expiryTimeStamp;
112  }
113
114  public int getMasterKeyId() {
115    return this.masterKeyId;
116  }
117
118  public Priority getPriority() {
119    return this.priority;
120  }
121
122  public long getCreationTime() {
123    return this.creationTime;
124  }
125  /**
126   * Get the RMIdentifier of RM in which containers are allocated
127   * @return RMIdentifier
128   */
129  public long getRMIdentifer() {
130    return this.rmIdentifier;
131  }
132
133  public LogAggregationContext getLogAggregationContext() {
134    return this.logAggregationContext;
135  }
136
137  @Override
138  public void write(DataOutput out) throws IOException {
139    LOG.debug("Writing ContainerTokenIdentifier to RPC layer: " + this);
140    ApplicationAttemptId applicationAttemptId = this.containerId
141        .getApplicationAttemptId();
142    ApplicationId applicationId = applicationAttemptId.getApplicationId();
143    out.writeLong(applicationId.getClusterTimestamp());
144    out.writeInt(applicationId.getId());
145    out.writeInt(applicationAttemptId.getAttemptId());
146    out.writeLong(this.containerId.getContainerId());
147    out.writeUTF(this.nmHostAddr);
148    out.writeUTF(this.appSubmitter);
149    out.writeInt(this.resource.getMemory());
150    out.writeInt(this.resource.getVirtualCores());
151    out.writeLong(this.expiryTimeStamp);
152    out.writeInt(this.masterKeyId);
153    out.writeLong(this.rmIdentifier);
154    out.writeInt(this.priority.getPriority());
155    out.writeLong(this.creationTime);
156  }
157
158  @Override
159  public void readFields(DataInput in) throws IOException {
160    ApplicationId applicationId =
161        ApplicationId.newInstance(in.readLong(), in.readInt());
162    ApplicationAttemptId applicationAttemptId =
163        ApplicationAttemptId.newInstance(applicationId, in.readInt());
164    this.containerId =
165        ContainerId.newContainerId(applicationAttemptId, in.readLong());
166    this.nmHostAddr = in.readUTF();
167    this.appSubmitter = in.readUTF();
168    int memory = in.readInt();
169    int vCores = in.readInt();
170    this.resource = Resource.newInstance(memory, vCores);
171    this.expiryTimeStamp = in.readLong();
172    this.masterKeyId = in.readInt();
173    this.rmIdentifier = in.readLong();
174    this.priority = Priority.newInstance(in.readInt());
175    this.creationTime = in.readLong();
176  }
177
178  @Override
179  public Text getKind() {
180    return KIND;
181  }
182
183  @Override
184  public UserGroupInformation getUser() {
185    return UserGroupInformation.createRemoteUser(this.containerId.toString());
186  }
187
188  // TODO: Needed?
189  @InterfaceAudience.Private
190  public static class Renewer extends Token.TrivialRenewer {
191    @Override
192    protected Text getKind() {
193      return KIND;
194    }
195  }
196}