-
Notifications
You must be signed in to change notification settings - Fork 3.4k
HBASE-28432 Refactor tools which are under test packaging to a new mo… #6249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
hbase-common/src/test/java/org/apache/hadoop/hbase/io/crypto/KeyProviderForTestingCopy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * 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.hadoop.hbase.io.crypto; | ||
|
|
||
| import java.security.Key; | ||
| import javax.crypto.spec.SecretKeySpec; | ||
|
|
||
| /** | ||
| * Return a fixed secret key for AES for testing. | ||
| */ | ||
| public class KeyProviderForTestingCopy implements KeyProvider { | ||
|
|
||
| @Override | ||
| public void init(String parameters) { | ||
| } | ||
|
|
||
| @Override | ||
| public Key getKey(String name) { | ||
| return new SecretKeySpec(Encryption.hash128(name), "AES"); | ||
| } | ||
|
|
||
| @Override | ||
| public Key[] getKeys(String[] aliases) { | ||
| Key[] result = new Key[aliases.length]; | ||
| for (int i = 0; i < aliases.length; i++) { | ||
| result[i] = new SecretKeySpec(Encryption.hash128(aliases[i]), "AES"); | ||
| } | ||
| return result; | ||
| } | ||
| } |
227 changes: 227 additions & 0 deletions
227
hbase-common/src/test/java/org/apache/hadoop/hbase/util/RandomDistributionCopy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| /* | ||
| * 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.hadoop.hbase.util; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Random; | ||
|
|
||
| /** | ||
| * A class that generates random numbers that follow some distribution. | ||
| * <p> | ||
| * Copied from <a href="https://issues.apache.org/jira/browse/HADOOP-3315">hadoop-3315 tfile</a>. | ||
| * Remove after tfile is committed and use the tfile version of this class instead. | ||
| * </p> | ||
| */ | ||
| public class RandomDistributionCopy { | ||
| /** | ||
| * Interface for discrete (integer) random distributions. | ||
| */ | ||
| public interface DiscreteRNG { | ||
| /** | ||
| * Get the next random number | ||
| * @return the next random number. | ||
| */ | ||
| int nextInt(); | ||
| } | ||
|
|
||
| /** | ||
| * P(i)=1/(max-min) | ||
| */ | ||
| public static final class Flat implements DiscreteRNG { | ||
| private final Random random; | ||
| private final int min; | ||
| private final int max; | ||
|
|
||
| /** | ||
| * Generate random integers from min (inclusive) to max (exclusive) following even distribution. | ||
| * The basic random number generator. Minimum integer maximum integer (exclusive). | ||
| */ | ||
| public Flat(Random random, int min, int max) { | ||
| if (min >= max) { | ||
| throw new IllegalArgumentException("Invalid range"); | ||
| } | ||
| this.random = random; | ||
| this.min = min; | ||
| this.max = max; | ||
| } | ||
|
|
||
| /** | ||
| * @see DiscreteRNG#nextInt() | ||
| */ | ||
| @Override | ||
| public int nextInt() { | ||
| return random.nextInt(max - min) + min; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Zipf distribution. The ratio of the probabilities of integer i and j is defined as follows: | ||
| * P(i)/P(j)=((j-min+1)/(i-min+1))^sigma. | ||
| */ | ||
| public static final class Zipf implements DiscreteRNG { | ||
| private static final double DEFAULT_EPSILON = 0.001; | ||
| private final Random random; | ||
| private final ArrayList<Integer> k; | ||
| private final ArrayList<Double> v; | ||
|
|
||
| /** | ||
| * Constructor The random number generator. minimum integer (inclusvie) maximum integer | ||
| * (exclusive) parameter sigma. (sigma > 1.0) | ||
| */ | ||
| public Zipf(Random r, int min, int max, double sigma) { | ||
| this(r, min, max, sigma, DEFAULT_EPSILON); | ||
| } | ||
|
|
||
| /** | ||
| * Constructor. The random number generator. minimum integer (inclusvie) maximum integer | ||
| * (exclusive) parameter sigma. (sigma > 1.0) Allowable error percentage (0 < epsilon < 1.0). | ||
| */ | ||
| public Zipf(Random r, int min, int max, double sigma, double epsilon) { | ||
| if ((max <= min) || (sigma <= 1) || (epsilon <= 0) || (epsilon >= 0.5)) { | ||
| throw new IllegalArgumentException("Invalid arguments"); | ||
| } | ||
| random = r; | ||
| k = new ArrayList<>(); | ||
| v = new ArrayList<>(); | ||
|
|
||
| double sum = 0; | ||
| int last = -1; | ||
| for (int i = min; i < max; ++i) { | ||
| sum += Math.exp(-sigma * Math.log(i - min + 1)); | ||
| if ((last == -1) || i * (1 - epsilon) > last) { | ||
| k.add(i); | ||
| v.add(sum); | ||
| last = i; | ||
| } | ||
| } | ||
|
|
||
| if (last != max - 1) { | ||
| k.add(max - 1); | ||
| v.add(sum); | ||
| } | ||
|
|
||
| v.set(v.size() - 1, 1.0); | ||
|
|
||
| for (int i = v.size() - 2; i >= 0; --i) { | ||
| v.set(i, v.get(i) / sum); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @see DiscreteRNG#nextInt() | ||
| */ | ||
| @Override | ||
| public int nextInt() { | ||
| double d = random.nextDouble(); | ||
| int idx = Collections.binarySearch(v, d); | ||
|
|
||
| if (idx > 0) { | ||
| ++idx; | ||
| } else { | ||
| idx = -(idx + 1); | ||
| } | ||
|
|
||
| if (idx >= v.size()) { | ||
| idx = v.size() - 1; | ||
| } | ||
|
|
||
| if (idx == 0) { | ||
| return k.get(0); | ||
| } | ||
|
|
||
| int ceiling = k.get(idx); | ||
| int lower = k.get(idx - 1); | ||
|
|
||
| return ceiling - random.nextInt(ceiling - lower); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Binomial distribution. P(k)=select(n, k)*p^k*(1-p)^(n-k) (k = 0, 1, ..., n) | ||
| * P(k)=select(max-min-1, k-min)*p^(k-min)*(1-p)^(k-min)*(1-p)^(max-k-1) | ||
| */ | ||
| public static final class Binomial implements DiscreteRNG { | ||
| private final Random random; | ||
| private final int min; | ||
| private final int n; | ||
| private final double[] v; | ||
|
|
||
| private static double select(int n, int k) { | ||
| double ret = 1.0; | ||
| for (int i = k + 1; i <= n; ++i) { | ||
| ret *= (double) i / (i - k); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| private static double power(double p, int k) { | ||
| return Math.exp(k * Math.log(p)); | ||
| } | ||
|
|
||
| /** | ||
| * Generate random integers from min (inclusive) to max (exclusive) following Binomial | ||
| * distribution. The basic random number generator. Minimum integer maximum integer (exclusive). | ||
| * parameter. | ||
| */ | ||
| public Binomial(Random random, int min, int max, double p) { | ||
| if (min >= max) { | ||
| throw new IllegalArgumentException("Invalid range"); | ||
| } | ||
| this.random = random; | ||
| this.min = min; | ||
| this.n = max - min - 1; | ||
| if (n > 0) { | ||
| v = new double[n + 1]; | ||
| double sum = 0.0; | ||
| for (int i = 0; i <= n; ++i) { | ||
| sum += select(n, i) * power(p, i) * power(1 - p, n - i); | ||
| v[i] = sum; | ||
| } | ||
| for (int i = 0; i <= n; ++i) { | ||
| v[i] /= sum; | ||
| } | ||
| } else { | ||
| v = null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @see DiscreteRNG#nextInt() | ||
| */ | ||
| @Override | ||
| public int nextInt() { | ||
| if (v == null) { | ||
| return min; | ||
| } | ||
| double d = random.nextDouble(); | ||
| int idx = Arrays.binarySearch(v, d); | ||
| if (idx > 0) { | ||
| ++idx; | ||
| } else { | ||
| idx = -(idx + 1); | ||
| } | ||
|
|
||
| if (idx >= v.length) { | ||
| idx = v.length - 1; | ||
| } | ||
| return idx + min; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bad name.
Do we need two copies of this class ? Can't we just move this from hbase-compression to hbase-common with the same name ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same applies to the other "Copy" classes.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @stoty Thanks for having a look at the PR.
These files are not supposed to be bundled and are here only for draft change. please see previous comment where I have captured a summary of changes: #6249 (comment)
See
Also see
I have created copy to show case how these files are being used across the project. Just want to discuss
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am +1 on moving to hbase-common. Will wait some time before updating draft with this change, lets also see what other think about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a separete patch on top of this one which moves the classes to hbase-common ?
You can open a separate PR for that one, so this one still shows what you wanted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see how big the patch is without the copies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure @stoty let me post a new PR with suggested change.