r/m unused code dealing with hashes

git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@758995 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Ellis 2009-03-27 02:44:05 +00:00
parent 2aa0bba0d5
commit 4cff66a0ee
5 changed files with 1 additions and 337 deletions

View File

@ -1,64 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.service;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;
import org.apache.cassandra.io.SSTable;
import org.apache.log4j.Logger;
public final class BootstrapAndLbHelper
{
private static final Logger logger_ = Logger.getLogger(BootstrapAndLbHelper.class);
private static List<String> getIndexedPrimaryKeys()
{
List<String> indexedPrimaryKeys = SSTable.getIndexedKeys();
Iterator<String> it = indexedPrimaryKeys.iterator();
while ( it.hasNext() )
{
String key = it.next();
if ( !StorageService.instance().isPrimary(key) )
{
it.remove();
}
}
return indexedPrimaryKeys;
}
/**
* Given the number of keys that need to be transferred say, 1000
* and given the smallest key stored we need the hash of the 1000th
* key greater than the smallest key in the sorted order in the primary
* range.
*
* @param keyCount number of keys after which token is required.
* @return token.
*/
public static BigInteger getTokenBasedOnPrimaryCount(int keyCount)
{
List<String> indexedPrimaryKeys = getIndexedPrimaryKeys();
int index = keyCount / SSTable.indexInterval();
String key = (index >= indexedPrimaryKeys.size()) ? indexedPrimaryKeys.get( indexedPrimaryKeys.size() - 1 ) : indexedPrimaryKeys.get(index);
logger_.debug("Hashing key " + key + " ...");
return StorageService.instance().hash(key);
}
}

View File

@ -1095,12 +1095,6 @@ public final class StorageService implements IEndPointStateChangeSubscriber, Sto
doBootstrap(nodes);
}
public String getAppropriateToken(int count)
{
BigInteger token = BootstrapAndLbHelper.getTokenBasedOnPrimaryCount(count);
return token.toString();
}
public void doGC()
{
List<String> tables = DatabaseDescriptor.getTables();

View File

@ -55,16 +55,7 @@ public interface StorageServiceMBean
*
*/
public void doGC();
/**
* Get the token such that the range of this node
* is split after <i>count</i> number of keys.
* @param count number of keys after which to generate
* token.
* @return appropriate token
*/
public String getAppropriateToken(int count);
/**
* Stream the files in the bootstrap directory over to the
* node being bootstrapped. This is used in case of normal

View File

@ -1,107 +0,0 @@
package org.apache.cassandra.tools;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import org.apache.cassandra.net.EndPoint;
import org.apache.cassandra.net.Message;
import org.apache.cassandra.net.MessagingService;
import org.apache.cassandra.service.StorageService;
public class ClusterTool
{
public static final String SET_TOKEN = "settoken";
public static final String HASH_KEY = "hash";
public static final String BUILD_INDEX = "build_index";
public static final String READ_TEST = "read_test";
public static final String WRITE_TEST = "write_test";
public static void applyToken(String serverName, BigInteger token) throws IOException
{
try
{
EndPoint from = new EndPoint(InetAddress.getLocalHost().getHostName(), 7000);
System.out.println("Updating token of server " + serverName + " with token " + token);
Message message = new Message(from, "", StorageService.tokenVerbHandler_, new Object[]{ token.toByteArray() });
EndPoint ep = new EndPoint(serverName, 7000);
MessagingService.getMessagingInstance().sendOneWay(message, ep);
Thread.sleep(1000);
System.out.println("Successfully calibrated " + serverName);
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
public static void printUsage()
{
System.out.println("Usage: java -jar <cassandra-tools.jar> <command> <options>");
System.out.println("Commands:");
System.out.println("\t" + SET_TOKEN + " <server> <token>");
System.out.println("\t" + HASH_KEY + " <key>");
System.out.println("\t" + BUILD_INDEX + " <full path to the data file>");
System.out.println("\t" + READ_TEST + " <number of threads> <requests per sec per thread> <machine(s) to read (':' separated list)>");
System.out.println("\t" + WRITE_TEST + " <number of threads> <requests per sec per thread> <machine(s) to write (':' separated list)>");
}
public static void main(String[] args) throws Exception
{
if(args.length < 2)
{
printUsage();
return;
}
int argc = 0;
try
{
/* set the token for a particular node in the Cassandra cluster */
if(SET_TOKEN.equals(args[argc]))
{
String serverName = args[argc + 1];
BigInteger token = new BigInteger(args[argc + 2]);
//System.out.println("Calibrating " + serverName + " with token " + token);
applyToken(serverName, token);
}
/* Print the hash of a given key */
else if(HASH_KEY.equals(args[argc]))
{
System.out.println("Hash = [" + StorageService.hash(args[argc + 1]) + "]");
}
/* build indexes given the data file */
else if(BUILD_INDEX.equals(args[argc]))
{
IndexBuilder.main(args);
}
/* test reads */
else if(READ_TEST.equals(args[argc]))
{
System.out.println("Testing reads...");
int numThreads = Integer.parseInt(args[argc + 1]);
int rpsPerThread = Integer.parseInt(args[argc + 2]);
String machinesToRead = args[argc + 3];
// ReadTest.runReadTest(numThreads, rpsPerThread, machinesToRead);
}
/* test writes */
else if(WRITE_TEST.equals(args[argc]))
{
System.out.println("Testing writes...");
int numThreads = Integer.parseInt(args[argc + 1]);
int rpsPerThread = Integer.parseInt(args[argc + 2]);
String machinesToWrite = args[argc + 3];
// WriteTest.runWriteTest(numThreads, rpsPerThread, machinesToWrite);
}
} catch(Exception e)
{
System.err.println("Exception " + e.getMessage());
e.printStackTrace(System.err);
printUsage();
}
System.exit(0);
}
}

View File

@ -1,150 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.tools;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import org.apache.cassandra.io.DataInputBuffer;
import org.apache.cassandra.io.DataOutputBuffer;
import org.apache.cassandra.io.IFileReader;
import org.apache.cassandra.io.SSTable;
import org.apache.cassandra.io.SequenceFile;
import org.apache.cassandra.service.StorageService;
import org.apache.cassandra.utils.LogUtil;
import org.apache.log4j.Logger;
import org.apache.cassandra.io.*;
import org.apache.cassandra.utils.*;
public class FileSizeTokenGenerator
{
private static Logger logger_ = Logger.getLogger(IndexBuilder.class);
public static void main(String[] args)
{
if ( args.length != 4 )
{
System.out.println("Usage : java com.facebook.infrastructure.tools.IndexBuilder <full path to the data file> < split factor>");
System.exit(1);
}
try
{
int splitCount = Integer.parseInt(args[3]);
BigInteger l = new BigInteger(args[1]);
BigInteger h = new BigInteger(args[2]);
long totalSize = getTotalSize(args[0], l, h);
System.out.println(" Total Size : " + totalSize);
BigInteger[] tokens = generateTokens(args[0], l, h, totalSize, splitCount);
int i = 0 ;
for( BigInteger token : tokens)
{
System.out.println(i++ + " th Token " + token);
}
}
catch( Throwable th )
{
logger_.warn(LogUtil.throwableToString(th));
}
}
private static long getTotalSize(String dataFile, BigInteger l , BigInteger h) throws IOException
{
final int bufferSize = 64*1024;
IFileReader dataReader = SequenceFile.bufferedReader(dataFile, bufferSize);
DataOutputBuffer bufOut = new DataOutputBuffer();
DataInputBuffer bufIn = new DataInputBuffer();
long totalSize = 0;
try
{
while ( !dataReader.isEOF() )
{
bufOut.reset();
/* Record the position of the key. */
dataReader.next(bufOut);
bufIn.reset(bufOut.getData(), bufOut.getLength());
/* Key just read */
String key = bufIn.readUTF();
if ( !key.equals(SSTable.blockIndexKey_) && l.compareTo(StorageService.hash(key)) < 0 && h.compareTo(StorageService.hash(key)) > 0 )
{
int sz = bufIn.readInt();
byte[] keyData = new byte[sz];
bufIn.read(keyData, 0, sz);
totalSize= totalSize + sz;
}
}
}
finally
{
dataReader.close();
}
return totalSize;
}
private static BigInteger[] generateTokens(String dataFile,BigInteger l , BigInteger h, long totalSize, int splitCount) throws IOException
{
final int bufferSize = 64*1024;
IFileReader dataReader = SequenceFile.bufferedReader(dataFile, bufferSize);
DataOutputBuffer bufOut = new DataOutputBuffer();
DataInputBuffer bufIn = new DataInputBuffer();
long splitFactor = totalSize/(splitCount+1);
long curSize = 0;
BigInteger[] tokens = new BigInteger[splitCount];
int k = 0 ;
try
{
while ( !dataReader.isEOF())
{
bufOut.reset();
/* Record the position of the key. */
dataReader.next(bufOut);
bufIn.reset(bufOut.getData(), bufOut.getLength());
/* Key just read */
String key = bufIn.readUTF();
if ( !key.equals(SSTable.blockIndexKey_) && l.compareTo(StorageService.hash(key)) < 0 && h.compareTo(StorageService.hash(key)) > 0 )
{
int sz = bufIn.readInt();
curSize = curSize + sz;
byte[] keyData = new byte[sz];
bufIn.read(keyData, 0, sz);
if( curSize > splitFactor)
{
tokens[k++] = StorageService.hash(key);
curSize = 0 ;
if( k == splitCount)
{
break;
}
}
}
}
}
finally
{
dataReader.close();
}
return tokens;
}
}