Skip to content

Commit c199bcc

Browse files
Invert address translation table: map public addresses to private (#676)
## Usage and product changes NOTE: The address translation table now represents mapping _from_ the desired connection addresses to the addresses the cloud servers are configured with. This change does not impact users of TypeDB Core or TypeDB Cloud through the TypeDB Cloud Platform (https://cloud.typedb.com/) --------- Co-authored-by: Haikal Pribadi <[email protected]>
1 parent 295e7c6 commit c199bcc

File tree

35 files changed

+217
-165
lines changed

35 files changed

+217
-165
lines changed

.factory/automation.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ config:
2626
build:
2727
quality:
2828
filter:
29-
owner: vaticle
29+
owner: typedb
3030
branch: [master, development]
3131
dependency-analysis:
3232
image: vaticle-ubuntu-22.04
@@ -125,7 +125,7 @@ build:
125125
machine: 8-core-32-gb
126126
image: vaticle-ubuntu-20.04 # Ubuntu 20.04 has GLIBC version 2.31 (2020) which we should verify to compile against
127127
filter:
128-
owner: vaticle
128+
owner: typedb
129129
branch: [master, development]
130130
dependencies:
131131
- build
@@ -149,7 +149,7 @@ build:
149149
machine: 8-core-32-gb
150150
image: vaticle-ubuntu-20.04 # Ubuntu 20.04 has GLIBC version 2.31 (2020) which we should verify to compile against
151151
filter:
152-
owner: vaticle
152+
owner: typedb
153153
branch: [master, development]
154154
dependencies:
155155
- build
@@ -509,7 +509,7 @@ build:
509509
sync-dependencies:
510510
image: vaticle-ubuntu-22.04
511511
filter:
512-
owner: vaticle
512+
owner: typedb
513513
branch: [master, development]
514514
dependencies:
515515
- build
@@ -546,7 +546,7 @@ build:
546546

547547
release:
548548
filter:
549-
owner: vaticle
549+
owner: typedb
550550
branch: [master]
551551
validation:
552552
validate-dependencies:

c/src/concept/concept.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
use std::ffi::c_char;
2121

22-
use chrono::NaiveDateTime;
22+
use chrono::DateTime;
2323
use typedb_driver::{
2424
concept::{
2525
Annotation, Attribute, AttributeType, Concept, Entity, EntityType, Relation, RelationType, RoleType, Value,
@@ -56,7 +56,7 @@ pub extern "C" fn value_new_string(string: *const c_char) -> *mut Concept {
5656
/// Creates a new ``Value`` object of the specified datetime value.
5757
#[no_mangle]
5858
pub extern "C" fn value_new_date_time_from_millis(millis: i64) -> *mut Concept {
59-
release(Concept::Value(Value::DateTime(NaiveDateTime::from_timestamp_millis(millis).unwrap())))
59+
release(Concept::Value(Value::DateTime(DateTime::from_timestamp_millis(millis).unwrap().naive_utc())))
6060
}
6161

6262
/// Returns <code>true</code> if the value which this ``Value`` concept holds is of type <code>boolean</code>.
@@ -143,7 +143,7 @@ pub extern "C" fn value_get_string(value: *const Concept) -> *mut c_char {
143143
#[no_mangle]
144144
pub extern "C" fn value_get_date_time_as_millis(value: *const Concept) -> i64 {
145145
if let Value::DateTime(date_time) = borrow_as_value(value) {
146-
date_time.timestamp_millis()
146+
date_time.and_utc().timestamp_millis()
147147
} else {
148148
unreachable!("Attempting to unwrap a non-datetime {:?} as datetime", borrow_as_value(value))
149149
}

c/src/connection.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ pub extern "C" fn connection_open_cloud(
5252
/// Open a TypeDB Driver to TypeDB Cloud server(s), using provided address translation, with
5353
/// the provided credential.
5454
///
55-
/// @param advertised_addresses A null-terminated array holding the address(es) the TypeDB server(s)
56-
/// are configured to advertise
57-
/// @param translated_addresses A null-terminated array holding the address(es) of the TypeDB server(s)
55+
/// @param public_addresses A null-terminated array holding the address(es) of the TypeDB server(s)
5856
/// the driver will connect to. This array <i>must</i> have the same length as <code>advertised_addresses</code>
57+
/// @param private_addresses A null-terminated array holding the address(es) the TypeDB server(s)
58+
/// are configured to advertise
5959
/// @param credential The <code>Credential</code> to connect with
6060
#[no_mangle]
6161
pub extern "C" fn connection_open_cloud_translated(
62-
advertised_addresses: *const *const c_char,
63-
translated_addresses: *const *const c_char,
62+
public_addresses: *const *const c_char,
63+
private_addresses: *const *const c_char,
6464
credential: *const Credential,
6565
) -> *mut Connection {
66-
let addresses = string_array_view(advertised_addresses).zip_eq(string_array_view(translated_addresses)).collect();
66+
let addresses = string_array_view(public_addresses).zip_eq(string_array_view(private_addresses)).collect();
6767
try_release(Connection::new_cloud_with_translation(addresses, borrow(credential).clone()))
6868
}
6969

cpp/include/typedb/connection/driver.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ class Driver {
8585
* Driver::cloudDriver(addresses, credential);
8686
* </pre>
8787
*
88-
* @param addresses The address(es) of the TypeDB server(s) or translation map from addresses
89-
* received from the TypeDB server(s) to addresses to be used by the driver for connection
88+
* @param addresses The address(es) of the TypeDB server(s) or translation map from addresses to be used
89+
* by the driver for connection to addresses received from the TypeDB server(s)
9090
* @param credential The Credential to connect with
9191
*/
9292
static Driver cloudDriver(const std::vector<std::string>& addresses, const Credential& credential);

cpp/lib/connection/driver.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ Driver Driver::cloudDriver(const std::vector<std::string>& addresses, const Cred
4747
}
4848

4949
Driver Driver::cloudDriver(const std::unordered_map<std::string, std::string>& addressTranslation, const Credential& credential) {
50-
std::vector<const char*> advertisedAddressesNative;
51-
std::vector<const char*> translatedAddressesNative;
52-
for (auto& [advertised, translated] : addressTranslation) {
53-
advertisedAddressesNative.push_back(advertised.c_str());
54-
translatedAddressesNative.push_back(translated.c_str());
50+
std::vector<const char*> publicAddressesNative;
51+
std::vector<const char*> privateAddressesNative;
52+
for (auto& [publicAddress, privateAddress] : addressTranslation) {
53+
publicAddressesNative.push_back(publicAddress.c_str());
54+
privateAddressesNative.push_back(privateAddress.c_str());
5555
}
56-
advertisedAddressesNative.push_back(nullptr);
57-
translatedAddressesNative.push_back(nullptr);
56+
publicAddressesNative.push_back(nullptr);
57+
privateAddressesNative.push_back(nullptr);
5858
auto p = _native::connection_open_cloud_translated(
59-
advertisedAddressesNative.data(),
60-
translatedAddressesNative.data(),
59+
publicAddressesNative.data(),
60+
privateAddressesNative.data(),
6161
credential.getNative()
6262
);
6363
DriverException::check_and_throw();

cpp/test/behaviour/steps/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,4 @@ int main(int argc, char** argv) {
5656
&TypeDB::BDD::testHooks);
5757
driver.loadFeature(argv[1]);
5858
return driver.runAllTests();
59-
return 0;
6059
}

cpp/test/cucumber/include/cucumber_bdd/runner.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class TestRunner : public TestRunnerBase {
7171
void afterAllTests() override {
7272
if (hooks != nullptr) hooks->afterAll();
7373
}
74+
7475
bool skipScenario(const cucumber::messages::pickle& scenario) override {
7576
return (hooks != nullptr) ? hooks->skipScenario(scenario) : false;
7677
}

cpp/test/cucumber/lib/runner.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void TestRunnerBase::loadFeature(const std::string& path) {
5151

5252
if (doc.feature.has_value()) {
5353
for (cucumber::messages::pickle scenario : compiler.compile(doc, path)) {
54-
if (skipScenario(scenario)) {
54+
if (skipScenario(scenario) || scenario.steps.empty()) {
5555
DEBUGONLY(std::cout << "Skipping scenario: " << scenario.name << std::endl)
5656
} else {
5757
DEBUGONLY(std::cout << "Registering scenario: " << scenario.name << std::endl)
@@ -64,8 +64,9 @@ void TestRunnerBase::loadFeature(const std::string& path) {
6464

6565
int TestRunnerBase::runAllTests() {
6666
beforeAllTests();
67-
return RUN_ALL_TESTS();
67+
int ret = RUN_ALL_TESTS();
6868
afterAllTests();
69+
return ret;
6970
}
7071

7172
} // namespace cucumber_bdd

csharp/Connection/TypeDBDriver.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ private static Pinvoke.Connection OpenCloud(IDictionary<string, string> addressT
8080
{
8181
try
8282
{
83-
string[] advertisedAddresses = new string[addressTranslation.Count];
84-
string[] translatedAddresses = new string[addressTranslation.Count];
83+
string[] publicAddresses = new string[addressTranslation.Count];
84+
string[] privateAddresses = new string[addressTranslation.Count];
8585
int index = 0;
8686
foreach (KeyValuePair<string, string> translation in addressTranslation)
8787
{
88-
advertisedAddresses[index] = translation.Key;
89-
translatedAddresses[index] = translation.Value;
88+
publicAddresses[index] = translation.Key;
89+
privateAddresses[index] = translation.Value;
9090
index++;
9191
}
92-
return Pinvoke.typedb_driver.connection_open_cloud_translated(advertisedAddresses, translatedAddresses, credential.NativeObject);
92+
return Pinvoke.typedb_driver.connection_open_cloud_translated(publicAddresses, privateAddresses, credential.NativeObject);
9393
}
9494
catch (Pinvoke.Error e)
9595
{

csharp/Drivers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static ITypeDBDriver CloudDriver(string address, TypeDBCredential credent
7171
* </pre>
7272
*
7373
* @param addresses The address(es) of the TypeDB server(s) or translation map from addresses
74-
* received from the TypeDB server(s) to addresses to be used by the driver for connection
74+
* to be used by the driver for connection to addresses received from the TypeDB server(s)
7575
* @param credential The credential to connect with
7676
*/
7777
public static ITypeDBDriver CloudDriver(ICollection<string> addresses, TypeDBCredential credential)

docs/modules/ROOT/partials/c/connection/connection.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ a| `credential` a| The ``Credential`` to connect with a| `const struct Credentia
8888
8989
[source,cpp]
9090
----
91-
struct Connection* connection_open_cloud_translated(const char*const* advertised_addresses, const char*const* translated_addresses, const struct Credential* credential)
91+
struct Connection* connection_open_cloud_translated(const char*const* public_addresses, const char*const* private_addresses, const struct Credential* credential)
9292
----
9393
9494
@@ -102,8 +102,8 @@ Open a TypeDB Driver to TypeDB Cloud server(s), using provided address translati
102102
[options="header"]
103103
|===
104104
|Name |Description |Type
105-
a| `advertised_addresses` a| A null-terminated array holding the address(es) the TypeDB server(s) are configured to advertise a| `const char*const*`
106-
a| `translated_addresses` a| A null-terminated array holding the address(es) of the TypeDB server(s) the driver will connect to. This array _must_ have the same length as ``advertised_addresses`` a| `const char*const*`
105+
a| `public_addresses` a| A null-terminated array holding the address(es) of the TypeDB server(s) the driver will connect to. This array _must_ have the same length as ``advertised_addresses`` a| `const char*const*`
106+
a| `private_addresses` a| A null-terminated array holding the address(es) the TypeDB server(s) are configured to advertise a| `const char*const*`
107107
a| `credential` a| The ``Credential`` to connect with a| `const struct Credential*`
108108
|===
109109

docs/modules/ROOT/partials/cpp/connection/Driver.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Open a TypeDB Driver to TypeDB Cloud server(s) available at the provided address
6363
[options="header"]
6464
|===
6565
|Name |Description |Type
66-
a| `addresses` a| The address(es) of the TypeDB server(s) or translation map from addresses received from the TypeDB server(s) to addresses to be used by the driver for connection a| `const std::vector< std::string >&`
66+
a| `addresses` a| The address(es) of the TypeDB server(s) or translation map from addresses to be used by the driver for connection to addresses received from the TypeDB server(s) a| `const std::vector< std::string >&`
6767
a| `credential` a| The Credential to connect with a| `const Credential&`
6868
|===
6969

docs/modules/ROOT/partials/csharp/connection/Drivers.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Open a TypeDB Driver to TypeDB Cloud server(s) available at the provided address
5757
[options="header"]
5858
|===
5959
|Name |Description |Type
60-
a| `addresses` a| The address(es) of the TypeDB server(s) or translation map from addresses received from the TypeDB server(s) to addresses to be used by the driver for connection a| `ICollection< string >`
60+
a| `addresses` a| The address(es) of the TypeDB server(s) or translation map from addresses to be used by the driver for connection to addresses received from the TypeDB server(s) a| `ICollection< string >`
6161
a| `credential` a| The credential to connect with a| `TypeDBCredential`
6262
|===
6363

docs/modules/ROOT/partials/java/connection/TypeDB.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Open a TypeDB Driver to TypeDB Cloud server(s), using provided address translati
113113
[options="header"]
114114
|===
115115
|Name |Description |Type
116-
a| `addressTranslation` a| Translation map from addresses received from the TypeDB server(s) to addresses to be used by the driver for connection a| `java.util.Map<java.lang.String,​java.lang.String>`
116+
a| `addressTranslation` a| Translation map from addresses to be used by the driver for connection to addresses received from the TypeDB server(s) a| `java.util.Map<java.lang.String,​java.lang.String>`
117117
a| `credential` a| The credential to connect with a| `TypeDBCredential`
118118
|===
119119

docs/modules/ROOT/partials/nodejs/connection/TypeDB.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Creates a connection to TypeDB Cloud, authenticating with the provided credentia
2929
[options="header"]
3030
|===
3131
|Name |Description |Type
32-
a| `addresses` a| List of addresses of the individual TypeDB Cloud servers. As long one specified address is provided, the driver will discover the other addresses from that server. a| `string \| string[] \| Record<string, string>`
32+
a| `addresses` a| List of addresses of the individual TypeDB Cloud servers. As long one specified address is provided, the driver will discover the other addresses from that server. Alternatively, a translation map from addresses to be used by the driver for connection to addresses received from the TypeDB server(s) may be provided. a| `string \| string[] \| Record<string, string>`
3333
a| `credential` a| The credentials to log in, and encryption settings. See ``TypeDBCredential``
3434
Examples
3535
``const driver = TypeDB.cloudDriver(["127.0.0.1:11729"], new TypeDBCredential(username, password));

docs/modules/ROOT/partials/rust/connection/Connection.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Creates a new TypeDB Cloud connection.
149149
[options="header"]
150150
|===
151151
|Name |Description |Type
152-
a| `address_translation` a| Translation map from addresses received from the TypeDB server(s) to addresses to be used by the driver for connection a| `HashMap<T`
152+
a| `address_translation` a| Translation map from addresses to be used by the driver for connection to addresses received from the TypeDB server(s) a| `HashMap<T`
153153
a| `credential` a| User credential and TLS encryption setting a| `Credential`
154154
|===
155155

docs/modules/ROOT/partials/rust/connection/ReplicaInfo.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The metadata and state of an individual raft replica of a database.
1616
|Name |Type |Description
1717
a| `is_preferred` a| `bool` a| Whether this is the preferred replica of the raft cluster. If true, Operations which can be run on any replica will prefer to use this replica.
1818
a| `is_primary` a| `bool` a| Whether this is the primary replica of the raft cluster.
19-
a| `server` a| `String` a| The server hosting this replica
19+
a| `server` a| `Address` a| The server hosting this replica
2020
a| `term` a| `i64` a| The raft protocol ‘term’ of this replica.
2121
|===
2222
// end::properties[]

java/TypeDB.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public static TypeDBDriver cloudDriver(Set<String> addresses, TypeDBCredential c
8686
* TypeDB.cloudDriver(addressTranslation, credential);
8787
* </pre>
8888
*
89-
* @param addressTranslation Translation map from addresses received from the TypeDB server(s)
90-
* to addresses to be used by the driver for connection
89+
* @param addressTranslation Translation map from addresses to be used by the driver for connection
90+
* to addresses received from the TypeDB server(s)
9191
* @param credential The credential to connect with
9292
*/
9393
public static TypeDBDriver cloudDriver(Map<String, String> addressTranslation, TypeDBCredential credential) {

java/connection/TypeDBDriverImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ private static com.vaticle.typedb.driver.jni.Connection openCloud(Set<String> in
8181

8282
private static com.vaticle.typedb.driver.jni.Connection openCloud(Map<String, String> addressTranslation, TypeDBCredential credential) {
8383
try {
84-
List<String> advertised = new ArrayList();
85-
List<String> translated = new ArrayList();
84+
List<String> publicAddresses = new ArrayList();
85+
List<String> privateAddresses = new ArrayList();
8686
for (Map.Entry<String, String> entry: addressTranslation.entrySet()) {
87-
advertised.add(entry.getKey());
88-
translated.add(entry.getValue());
87+
publicAddresses.add(entry.getKey());
88+
privateAddresses.add(entry.getValue());
8989
}
9090
return connection_open_cloud_translated(
91-
advertised.toArray(new String[0]),
92-
translated.toArray(new String[0]),
91+
publicAddresses.toArray(new String[0]),
92+
privateAddresses.toArray(new String[0]),
9393
credential.nativeObject
9494
);
9595
} catch (com.vaticle.typedb.driver.jni.Error e) {

nodejs/TypeDB.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export namespace TypeDB {
4242
* Creates a connection to TypeDB Cloud, authenticating with the provided credentials.
4343
* @param addresses - List of addresses of the individual TypeDB Cloud servers.
4444
* As long one specified address is provided, the driver will discover the other addresses from that server.
45+
* Alternatively, a translation map from addresses to be used by the driver for connection
46+
* to addresses received from the TypeDB server(s) may be provided.
4547
* @param credential - The credentials to log in, and encryption settings. See <code>{@link TypeDBCredential}</code>
4648
*
4749
* ### Examples

nodejs/connection/TypeDBDriverImpl.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,16 @@ export class TypeDBDriverImpl implements TypeDBDriver {
100100
} else {
101101
addressTranslation = this._initAddresses;
102102
const unknown = [];
103-
for (const [advertised, _] of Object.entries(addressTranslation)) {
104-
if (serverAddresses.indexOf(advertised) === -1) {
105-
unknown.push(advertised);
103+
for (const [_, privateAddress] of Object.entries(addressTranslation)) {
104+
if (serverAddresses.indexOf(privateAddress) === -1) {
105+
unknown.push(privateAddress);
106106
}
107107
}
108108
const unmapped = [];
109-
for (const advertisedAddress of serverAddresses) {
110-
if (!(advertisedAddress in addressTranslation)) {
111-
unmapped.push(advertisedAddress);
109+
for (const privateAddress of serverAddresses) {
110+
const publicAddress = Object.keys(addressTranslation).find((key) => addressTranslation[key] == privateAddress);
111+
if (!publicAddress) {
112+
unmapped.push(privateAddress);
112113
}
113114
}
114115
if (unknown.length > 0 || unmapped.length > 0) {
@@ -134,7 +135,13 @@ export class TypeDBDriverImpl implements TypeDBDriver {
134135
}
135136

136137
private async fetchCloudServerAddresses(): Promise<string[]> {
137-
for (const [_, address] of Object.entries(this._initAddresses)) {
138+
let initPrivateAddresses: string[];
139+
if (Array.isArray(this._initAddresses)) {
140+
initPrivateAddresses = this._initAddresses;
141+
} else {
142+
initPrivateAddresses = Array.from(Object.values(this._initAddresses));
143+
}
144+
for (const address of initPrivateAddresses) {
138145
try {
139146
const stub = new TypeDBStubImpl(address, this._credential);
140147
await stub.open();
@@ -145,7 +152,7 @@ export class TypeDBDriverImpl implements TypeDBDriver {
145152
console.error(`Fetching cloud servers from ${address} failed.`, e);
146153
}
147154
}
148-
throw new TypeDBDriverError(CLOUD_UNABLE_TO_CONNECT.message(Object.values(this._initAddresses).join(",")));
155+
throw new TypeDBDriverError(CLOUD_UNABLE_TO_CONNECT.message(initPrivateAddresses.join(",")));
149156
}
150157

151158
isOpen(): boolean {

0 commit comments

Comments
 (0)