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.server.protocol;
019    
020    /*
021     * A system administrator can tune the balancer bandwidth parameter
022     * (dfs.balance.bandwidthPerSec) dynamically by calling
023     * "dfsadmin -setBalanacerBandwidth newbandwidth".
024     * This class is to define the command which sends the new bandwidth value to
025     * each datanode.
026     */
027    
028    import java.io.DataInput;
029    import java.io.DataOutput;
030    import java.io.IOException;
031    
032    import org.apache.hadoop.io.Writable;
033    import org.apache.hadoop.io.WritableFactories;
034    import org.apache.hadoop.io.WritableFactory;
035    
036    /**
037     * Balancer bandwidth command instructs each datanode to change its value for
038     * the max amount of network bandwidth it may use during the block balancing
039     * operation.
040     * 
041     * The Balancer Bandwidth Command contains the new bandwidth value as its
042     * payload. The bandwidth value is in bytes per second.
043     */
044    public class BalancerBandwidthCommand extends DatanodeCommand {
045      private final static long BBC_DEFAULTBANDWIDTH = 0L;
046    
047      private long bandwidth;
048    
049      /**
050       * Balancer Bandwidth Command constructor. Sets bandwidth to 0.
051       */
052      BalancerBandwidthCommand() {
053        this(BBC_DEFAULTBANDWIDTH);
054      }
055    
056      /**
057       * Balancer Bandwidth Command constructor.
058       *
059       * @param bandwidth Blanacer bandwidth in bytes per second.
060       */
061      public BalancerBandwidthCommand(long bandwidth) {
062        super(DatanodeProtocol.DNA_BALANCERBANDWIDTHUPDATE);
063        this.bandwidth = bandwidth;
064      }
065    
066      /**
067       * Get current value of the max balancer bandwidth in bytes per second.
068       *
069       * @return bandwidth Blanacer bandwidth in bytes per second for this datanode.
070       */
071      public long getBalancerBandwidthValue() {
072        return this.bandwidth;
073      }
074    
075      // ///////////////////////////////////////////////
076      // Writable
077      // ///////////////////////////////////////////////
078      static { // register a ctor
079        WritableFactories.setFactory(BalancerBandwidthCommand.class, new WritableFactory() {
080          public Writable newInstance() {
081            return new BalancerBandwidthCommand();
082          }
083        });
084      }
085    
086      /**
087       * Writes the bandwidth payload to the Balancer Bandwidth Command packet.
088       * @param out DataOutput stream used for writing commands to the datanode.
089       * @throws IOException
090       */
091      public void write(DataOutput out) throws IOException {
092        super.write(out);
093        out.writeLong(this.bandwidth);
094      }
095    
096      /**
097       * Reads the bandwidth payload from the Balancer Bandwidth Command packet.
098       * @param in DataInput stream used for reading commands to the datanode.
099       * @throws IOException
100       */
101      public void readFields(DataInput in) throws IOException {
102        super.readFields(in);
103        this.bandwidth = in.readLong();
104      }
105    }