From e48dbb8d345d480349d53b48f167f7f463d2d574 Mon Sep 17 00:00:00 2001 From: Sumant Sahney Date: Sat, 10 Mar 2018 01:29:01 +0530 Subject: [PATCH 1/2] Added ByteConverter function and added unit tests for it --- .../apache/cassandra/utils/ByteConvertor.java | 28 ++++++++++++++ .../apache/cassandra/ByteConvertorTest.java | 37 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/java/org/apache/cassandra/utils/ByteConvertor.java create mode 100644 test/unit/org/apache/cassandra/ByteConvertorTest.java diff --git a/src/java/org/apache/cassandra/utils/ByteConvertor.java b/src/java/org/apache/cassandra/utils/ByteConvertor.java new file mode 100644 index 000000000000..9bda023b206d --- /dev/null +++ b/src/java/org/apache/cassandra/utils/ByteConvertor.java @@ -0,0 +1,28 @@ +/* + * 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.utils; + +public class ByteConvertor +{ + private static final long MEGABYTE = 1024L * 1024L; + + public static long bytesToMeg(long bytes) { + return bytes / MEGABYTE ; + } +} \ No newline at end of file diff --git a/test/unit/org/apache/cassandra/ByteConvertorTest.java b/test/unit/org/apache/cassandra/ByteConvertorTest.java new file mode 100644 index 000000000000..d9cb09f30ddd --- /dev/null +++ b/test/unit/org/apache/cassandra/ByteConvertorTest.java @@ -0,0 +1,37 @@ +/* + * 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; + +import org.junit.Test; +import org.apache.cassandra.utils.ByteConvertor; +import static junit.framework.Assert.assertEquals; + +public class ByteConvertorTest +{ + @Test + public void testByteToMegaByte() + { + int test1 = 100000000; + long test2= 200000000; + long test1_ans = 95; + long test2_ans = 190; + assertEquals(ByteConvertor.bytesToMeg(test1),test1_ans); + assertEquals(ByteConvertor.bytesToMeg(test2),test2_ans); + } +} From 5429c9aeae532b7541070bd9a317a085785694e6 Mon Sep 17 00:00:00 2001 From: Sumant Sahney Date: Sat, 10 Mar 2018 01:44:15 +0530 Subject: [PATCH 2/2] Changing Byte to MegaByte in logs --- src/java/org/apache/cassandra/db/Directories.java | 4 ++-- src/java/org/apache/cassandra/utils/memory/BufferPool.java | 6 +++--- tools/stress/src/org/apache/cassandra/stress/Stress.java | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/cassandra/db/Directories.java b/src/java/org/apache/cassandra/db/Directories.java index ae7700cc9544..c536c31cdc99 100644 --- a/src/java/org/apache/cassandra/db/Directories.java +++ b/src/java/org/apache/cassandra/db/Directories.java @@ -33,7 +33,6 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.function.BiFunction; import java.util.function.Consumer; - import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableMap; @@ -55,6 +54,7 @@ import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.FBUtilities; import org.apache.cassandra.utils.Pair; +import org.apache.cassandra.utils.ByteConvertor; /** * Encapsulate handling of paths to the data files. @@ -376,7 +376,7 @@ public DataDirectory getWriteableLocation(long writeSize) // exclude directory if its total writeSize does not fit to data directory if (candidate.availableSpace < writeSize) { - logger.trace("removing candidate {}, usable={}, requested={}", candidate.dataDirectory.location, candidate.availableSpace, writeSize); + logger.trace("removing candidate {}, usable={}, requested={} MB", candidate.dataDirectory.location, candidate.availableSpace, ByteConvertor.bytesToMeg(writeSize)); tooBig = true; continue; } diff --git a/src/java/org/apache/cassandra/utils/memory/BufferPool.java b/src/java/org/apache/cassandra/utils/memory/BufferPool.java index f9720594e908..83dcb12fad7e 100644 --- a/src/java/org/apache/cassandra/utils/memory/BufferPool.java +++ b/src/java/org/apache/cassandra/utils/memory/BufferPool.java @@ -38,7 +38,7 @@ import org.apache.cassandra.config.DatabaseDescriptor; import org.apache.cassandra.metrics.BufferPoolMetrics; import org.apache.cassandra.utils.concurrent.Ref; - +import org.apache.cassandra.utils.ByteConvertor; /** * A pool of ByteBuffers that can be recycled. */ @@ -115,7 +115,7 @@ private static ByteBuffer takeFromPool(int size, boolean allocateOnHeapWhenExhau return ret; if (logger.isTraceEnabled()) - logger.trace("Requested buffer size {} has been allocated directly due to lack of capacity", size); + logger.trace("Requested buffer size {} MB has been allocated directly due to lack of capacity", ByteConvertor.bytesToMeg(size)); return localPool.get().allocate(size, allocateOnHeapWhenExhausted); } @@ -131,7 +131,7 @@ private static ByteBuffer maybeTakeFromPool(int size, boolean allocateOnHeapWhen if (size > CHUNK_SIZE) { if (logger.isTraceEnabled()) - logger.trace("Requested buffer size {} is bigger than {}, allocating directly", size, CHUNK_SIZE); + logger.trace("Requested buffer size {} MB is bigger than {}, allocating directly", ByteConvertor.bytesToMeg(size), CHUNK_SIZE); return localPool.get().allocate(size, allocateOnHeapWhenExhausted); } diff --git a/tools/stress/src/org/apache/cassandra/stress/Stress.java b/tools/stress/src/org/apache/cassandra/stress/Stress.java index bc6d0273b322..b2ba1ec3c830 100644 --- a/tools/stress/src/org/apache/cassandra/stress/Stress.java +++ b/tools/stress/src/org/apache/cassandra/stress/Stress.java @@ -71,6 +71,7 @@ public static void main(String[] arguments) throws Exception e.printStackTrace(); return; } + PrintStream logout = settings.log.getOutput();