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    package org.apache.hadoop.hdfs.protocol;
019    
020    import java.util.Date;
021    
022    import org.apache.hadoop.classification.InterfaceAudience;
023    import org.apache.hadoop.classification.InterfaceStability;
024    
025    /**
026     * Rolling upgrade information
027     */
028    @InterfaceAudience.Private
029    @InterfaceStability.Evolving
030    public class RollingUpgradeInfo extends RollingUpgradeStatus {
031      private final long startTime;
032      private final long finalizeTime;
033      private boolean createdRollbackImages;
034      
035      public RollingUpgradeInfo(String blockPoolId, boolean createdRollbackImages,
036          long startTime, long finalizeTime) {
037        super(blockPoolId);
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      public boolean isFinalized() {
061        return finalizeTime != 0;
062      }
063    
064      public long getFinalizeTime() {
065        return finalizeTime;
066      }
067    
068      @Override
069      public int hashCode() {
070        //only use lower 32 bits
071        return super.hashCode() ^ (int)startTime ^ (int)finalizeTime;
072      }
073    
074      @Override
075      public boolean equals(Object obj) {
076        if (obj == this) {
077          return true;
078        } else if (obj == null || !(obj instanceof RollingUpgradeInfo)) {
079          return false;
080        }
081        final RollingUpgradeInfo that = (RollingUpgradeInfo)obj;
082        return super.equals(that)
083            && this.startTime == that.startTime
084            && this.finalizeTime == that.finalizeTime;
085      }
086    
087      @Override
088      public String toString() {
089        return super.toString()
090          +  "\n     Start Time: " + (startTime == 0? "<NOT STARTED>": timestamp2String(startTime))
091          +  "\n  Finalize Time: " + (finalizeTime == 0? "<NOT FINALIZED>": timestamp2String(finalizeTime));
092      }
093      
094      private static String timestamp2String(long timestamp) {
095        return new Date(timestamp) + " (=" + timestamp + ")";
096      }
097    
098      public static class Bean {
099        private final String blockPoolId;
100        private final long startTime;
101        private final long finalizeTime;
102        private final boolean createdRollbackImages;
103    
104        public Bean(RollingUpgradeInfo f) {
105          this.blockPoolId = f.getBlockPoolId();
106          this.startTime = f.startTime;
107          this.finalizeTime = f.finalizeTime;
108          this.createdRollbackImages = f.createdRollbackImages();
109        }
110    
111        public String getBlockPoolId() {
112          return blockPoolId;
113        }
114    
115        public long getStartTime() {
116          return startTime;
117        }
118    
119        public long getFinalizeTime() {
120          return finalizeTime;
121        }
122    
123        public boolean isCreatedRollbackImages() {
124          return createdRollbackImages;
125        }
126      }
127    }