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   */
068  public void finalize(long finalizeTime) {
069    if (finalizeTime != 0) {
070      this.finalizeTime = finalizeTime;
071      createdRollbackImages = false;
072    }
073  }
074
075  public long getFinalizeTime() {
076    return finalizeTime;
077  }
078
079  @Override
080  public int hashCode() {
081    //only use lower 32 bits
082    return super.hashCode() ^ (int)startTime ^ (int)finalizeTime;
083  }
084
085  @Override
086  public boolean equals(Object obj) {
087    if (obj == this) {
088      return true;
089    } else if (obj == null || !(obj instanceof RollingUpgradeInfo)) {
090      return false;
091    }
092    final RollingUpgradeInfo that = (RollingUpgradeInfo)obj;
093    return super.equals(that)
094        && this.startTime == that.startTime
095        && this.finalizeTime == that.finalizeTime;
096  }
097
098  @Override
099  public String toString() {
100    return super.toString()
101        +  "\n     Start Time: "
102        + (startTime == 0 ? "<NOT STARTED>" : timestamp2String(startTime))
103        +  "\n  Finalize Time: "
104        + (finalizeTime == 0 ? "<NOT FINALIZED>" :
105        timestamp2String(finalizeTime));
106  }
107
108  private static String timestamp2String(long timestamp) {
109    return new Date(timestamp) + " (=" + timestamp + ")";
110  }
111
112  public static class Bean {
113    private final String blockPoolId;
114    private final long startTime;
115    private final long finalizeTime;
116    private final boolean createdRollbackImages;
117
118    public Bean(RollingUpgradeInfo f) {
119      this.blockPoolId = f.getBlockPoolId();
120      this.startTime = f.startTime;
121      this.finalizeTime = f.finalizeTime;
122      this.createdRollbackImages = f.createdRollbackImages();
123    }
124
125    public String getBlockPoolId() {
126      return blockPoolId;
127    }
128
129    public long getStartTime() {
130      return startTime;
131    }
132
133    public long getFinalizeTime() {
134      return finalizeTime;
135    }
136
137    public boolean isCreatedRollbackImages() {
138      return createdRollbackImages;
139    }
140  }
141}