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 */
018package org.apache.hadoop.hdfs.protocol;
019
020import java.util.Date;
021
022import org.apache.hadoop.classification.InterfaceAudience;
023import org.apache.hadoop.classification.InterfaceStability;
024
025/**
026 * Rolling upgrade information
027 */
028@InterfaceAudience.Private
029@InterfaceStability.Evolving
030public class RollingUpgradeInfo extends RollingUpgradeStatus {
031  private final long startTime;
032  private long finalizeTime;
033  private boolean createdRollbackImages;
034  
035  public RollingUpgradeInfo(String blockPoolId, boolean createdRollbackImages,
036      long startTime, long finalizeTime) {
037    super(blockPoolId, finalizeTime != 0);
038    this.createdRollbackImages = createdRollbackImages;
039    this.startTime = startTime;
040    this.finalizeTime = finalizeTime;
041  }
042  
043  public boolean createdRollbackImages() {
044    return createdRollbackImages;
045  }
046
047  public void setCreatedRollbackImages(boolean created) {
048    this.createdRollbackImages = created;
049  }
050
051  public boolean isStarted() {
052    return startTime != 0;
053  }
054  
055  /** @return The rolling upgrade starting time. */
056  public long getStartTime() {
057    return startTime;
058  }
059
060  @Override
061  public boolean isFinalized() {
062    return finalizeTime != 0;
063  }
064
065  /**
066   * Finalize the upgrade if not already finalized
067   * @param finalizeTime
068   */
069  public void finalize(long finalizeTime) {
070    if (finalizeTime != 0) {
071      this.finalizeTime = finalizeTime;
072      createdRollbackImages = false;
073    }
074  }
075
076  public long getFinalizeTime() {
077    return finalizeTime;
078  }
079
080  @Override
081  public int hashCode() {
082    //only use lower 32 bits
083    return super.hashCode() ^ (int)startTime ^ (int)finalizeTime;
084  }
085
086  @Override
087  public boolean equals(Object obj) {
088    if (obj == this) {
089      return true;
090    } else if (obj == null || !(obj instanceof RollingUpgradeInfo)) {
091      return false;
092    }
093    final RollingUpgradeInfo that = (RollingUpgradeInfo)obj;
094    return super.equals(that)
095        && this.startTime == that.startTime
096        && this.finalizeTime == that.finalizeTime;
097  }
098
099  @Override
100  public String toString() {
101    return super.toString()
102      +  "\n     Start Time: " + (startTime == 0? "<NOT STARTED>": timestamp2String(startTime))
103      +  "\n  Finalize Time: " + (finalizeTime == 0? "<NOT FINALIZED>": timestamp2String(finalizeTime));
104  }
105  
106  private static String timestamp2String(long timestamp) {
107    return new Date(timestamp) + " (=" + timestamp + ")";
108  }
109
110  public static class Bean {
111    private final String blockPoolId;
112    private final long startTime;
113    private final long finalizeTime;
114    private final boolean createdRollbackImages;
115
116    public Bean(RollingUpgradeInfo f) {
117      this.blockPoolId = f.getBlockPoolId();
118      this.startTime = f.startTime;
119      this.finalizeTime = f.finalizeTime;
120      this.createdRollbackImages = f.createdRollbackImages();
121    }
122
123    public String getBlockPoolId() {
124      return blockPoolId;
125    }
126
127    public long getStartTime() {
128      return startTime;
129    }
130
131    public long getFinalizeTime() {
132      return finalizeTime;
133    }
134
135    public boolean isCreatedRollbackImages() {
136      return createdRollbackImages;
137    }
138  }
139}