-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from ZhiHanZ/client-lb
feat: support client side load balancing
- Loading branch information
Showing
23 changed files
with
1,091 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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,48 @@ | ||
name: "Setup Stateful Cluster Linux" | ||
description: "Running stateful tests in cluster mode" | ||
inputs: | ||
version: | ||
description: "query and meta service version" | ||
required: true | ||
default: "1.2.616-nightly" | ||
target: | ||
description: "" | ||
required: true | ||
default: "x86_64-unknown-linux-gnu" | ||
runs: | ||
using: "composite" | ||
steps: | ||
|
||
- name: Minio Setup for (ubuntu-latest only) | ||
shell: bash | ||
run: | | ||
docker run -d --network host --name minio \ | ||
-e "MINIO_ACCESS_KEY=minioadmin" \ | ||
-e "MINIO_SECRET_KEY=minioadmin" \ | ||
-e "MINIO_ADDRESS=:9900" \ | ||
-v /tmp/data:/data \ | ||
-v /tmp/config:/root/.minio \ | ||
minio/minio server /data | ||
export AWS_ACCESS_KEY_ID=minioadmin | ||
export AWS_SECRET_ACCESS_KEY=minioadmin | ||
export AWS_EC2_METADATA_DISABLED=true | ||
aws --endpoint-url http://127.0.0.1:9900/ s3 mb s3://testbucket | ||
- name: Download binary and extract into target directory | ||
shell: bash | ||
run: | | ||
wget --progress=bar:force:noscroll https://github.com/datafuselabs/databend/releases/download/v${{ inputs.version }}/databend-v${{ inputs.version }}-${{ inputs.target }}.tar.gz | ||
mkdir -p ./databend | ||
tar -xzvf databend-v${{ inputs.version }}-${{ inputs.target }}.tar.gz -C ./databend | ||
rm databend-v${{ inputs.version }}-${{ inputs.target }}.tar.gz | ||
- name: Start Databend Cluster | ||
shell: bash | ||
run: | | ||
chmod +x ./databend/bin/databend-meta | ||
chmod +x ./databend/bin/databend-query | ||
chmod +x ./scripts/wait_tcp.py | ||
chmod +x ./scripts/deploy/deploy_cluster.sh | ||
./scripts/deploy/deploy_cluster.sh |
This file contains 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 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,39 @@ | ||
name: Databend Cluster Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- master | ||
pull_request: | ||
branches: | ||
- main | ||
- master | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ github.ref }} | ||
|
||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
distribution: 'temurin' | ||
java-version: '17' | ||
cache: 'maven' | ||
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} # Value of the GPG private key to import | ||
gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase | ||
|
||
- uses: ./.github/actions/setup_databend_cluster | ||
timeout-minutes: 15 | ||
with: | ||
version: '1.2.616-nightly' | ||
target: 'x86_64-unknown-linux-gnu' | ||
- name: Run Maven clean deploy with release profile | ||
run: mvn clean test | ||
env: | ||
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} |
This file contains 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 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 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 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 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 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
118 changes: 118 additions & 0 deletions
118
databend-jdbc/src/main/java/com/databend/jdbc/DatabendClientLoadBalancingPolicy.java
This file contains 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,118 @@ | ||
package com.databend.jdbc; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class DatabendClientLoadBalancingPolicy { | ||
static class DisabledPolicy extends DatabendClientLoadBalancingPolicy { | ||
@Override | ||
public String toString() { | ||
return "Disabled"; | ||
} | ||
// do nothing | ||
} | ||
static class RandomPolicy extends DatabendClientLoadBalancingPolicy { | ||
@Override | ||
protected URI pickUri(String query_id, DatabendNodes nodes) { | ||
List<URI> uris = nodes.getUris(); | ||
|
||
if (uris == null || uris.isEmpty()) { | ||
return null; | ||
} | ||
|
||
// Generate a deterministic integer based on the query_id | ||
int deterministicValue = getQueryHash(query_id); | ||
|
||
// Use the deterministic value to select a URI | ||
int index = Math.abs(deterministicValue) % uris.size(); | ||
|
||
return uris.get(index); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Random"; | ||
} | ||
} | ||
|
||
static class RoundRobinPolicy extends DatabendClientLoadBalancingPolicy { | ||
@Override | ||
protected URI pickUri(String query_id, DatabendNodes nodes) { | ||
List<URI> uris = nodes.getUris(); | ||
|
||
if (uris == null || uris.isEmpty()) { | ||
return null; | ||
} | ||
|
||
// Use round robin to select a URI | ||
int index = nodes.index.getAndUpdate(v -> v + 1 >= uris.size() ? 0 : v + 1); | ||
|
||
return uris.get(index); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RoundRobin"; | ||
} | ||
} | ||
/** | ||
* Policy that disable load balance and always use the first node. | ||
*/ | ||
public static final String DISABLED = "disabled"; | ||
/** | ||
* Policy to pick a node randomly from the list of available nodes. | ||
*/ | ||
public static final String RANDOM = "random"; | ||
|
||
/** | ||
* Policy to pick a node using Round Robin Algorithm | ||
*/ | ||
public static final String ROUND_ROBIN = "round_robin"; | ||
|
||
|
||
static DatabendClientLoadBalancingPolicy create(String name) { | ||
DatabendClientLoadBalancingPolicy policy; | ||
if (RANDOM.equalsIgnoreCase(name)) { | ||
policy = new RandomPolicy(); | ||
} else if (ROUND_ROBIN.equalsIgnoreCase(name)) { | ||
policy = new RoundRobinPolicy(); | ||
} else if (DISABLED.equalsIgnoreCase(name)) { | ||
policy = new DisabledPolicy(); | ||
} else { | ||
throw new IllegalArgumentException("Unknown load balancing policy: " + name); | ||
} | ||
return policy; | ||
} | ||
|
||
|
||
/** | ||
* Policy to pick a node based on the least loaded algorithm. | ||
* @param nodes the list of URIs to choose from | ||
* @return the URI to use | ||
*/ | ||
protected URI pickUri(String query_id, DatabendNodes nodes) { | ||
if (nodes == null || nodes.getUris() == null || nodes.getUris().isEmpty()) { | ||
return null; | ||
} | ||
return nodes.getUris().get(0); | ||
} | ||
|
||
|
||
/** | ||
* Get int hash value of given query id | ||
* @param query_id the query id used for choosing load balancing node | ||
* @return hash value of the query id | ||
*/ | ||
private static int getQueryHash(String query_id) { | ||
if (query_id.isEmpty()) { | ||
return 0; | ||
} | ||
int hash = 202011; // Using the seed value | ||
for (char c : query_id.toCharArray()) { | ||
hash = hash * 31 + c; | ||
} | ||
return hash; | ||
} | ||
|
||
} |
Oops, something went wrong.