Skip to content

Commit 84ec0a0

Browse files
committed
Use cluster role from show databases if available
1 parent ee74be8 commit 84ec0a0

File tree

5 files changed

+41
-24
lines changed

5 files changed

+41
-24
lines changed

src/browser/modules/DBMSInfo/DatabaseKernelInfo.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
} from 'shared/modules/commands/commandsDuck'
4242
import {
4343
Database,
44-
getClusterRole,
44+
getClusterRoleForDb,
4545
getDatabases,
4646
getEdition,
4747
getStoreSize,
@@ -133,12 +133,13 @@ export const DatabaseKernelInfo = ({
133133
}
134134

135135
const mapStateToProps = (state: any) => {
136+
const dbName = getUsedDbName(state)
136137
return {
137138
version: getRawVersion(state),
138139
edition: getEdition(state),
139-
dbName: getUsedDbName(state),
140+
dbName,
140141
storeSize: getStoreSize(state),
141-
role: getClusterRole(state),
142+
role: getClusterRoleForDb(state, dbName),
142143
databases: getDatabases(state)
143144
}
144145
}

src/shared/modules/dbMeta/dbMetaDuck.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import { versionHasEditorHistorySetting } from './utils'
2121
import { isConfigValFalsy } from 'services/bolt/boltHelpers'
2222
import { GlobalState } from 'shared/globalState'
2323
import { APP_START } from 'shared/modules/app/appDuck'
24+
import { extractServerInfo } from './utils'
2425
import { coerce, SemVer } from 'semver'
26+
import { gte } from 'lodash-es'
2527

2628
export const UPDATE_META = 'meta/UPDATE_META'
2729
export const PARSE_META = 'meta/PARSE_META'
@@ -33,6 +35,7 @@ export const DB_META_DONE = 'meta/DB_META_DONE'
3335

3436
export const SYSTEM_DB = 'system'
3537
export const VERSION_FOR_EDITOR_HISTORY_SETTING = '4.3.0'
38+
export const VERSION_FOR_CLUSTER_ROLE_IN_SHOW_DB = '4.3.0'
3639

3740
export const metaQuery = `
3841
CALL db.labels() YIELD label
@@ -51,7 +54,6 @@ MATCH ()-[]->() RETURN { name:'relationships', data: count(*)} AS result
5154

5255
export const serverInfoQuery =
5356
'CALL dbms.components() YIELD name, versions, edition'
54-
import { extractServerInfo } from './utils'
5557

5658
export function fetchMetaData() {
5759
return {
@@ -97,7 +99,7 @@ export const initialState = {
9799
properties: [],
98100
functions: [],
99101
procedures: [],
100-
role: null,
102+
role: null, // Used pre version 4.3 (before SHOW DATABASES had the role and we had to query for it)
101103
server: {
102104
version: null,
103105
edition: null,
@@ -162,7 +164,6 @@ export const getEdition = (state: GlobalState) => state[NAME].server.edition
162164
export const hasEdition = (state: any) =>
163165
state[NAME].server.edition !== initialState.server.edition
164166
export const getStoreSize = (state: any) => state[NAME].server.storeSize
165-
export const getClusterRole = (state: any) => state[NAME].role
166167
export const isEnterprise = (state: any) =>
167168
['enterprise'].includes(state[NAME].server.edition)
168169
export const isBeta = (state: any) => /-/.test(state[NAME].server.version)
@@ -219,6 +220,28 @@ export const shouldRetainConnectionCredentials = (state: any) =>
219220
export const shouldRetainEditorHistory = (state: any) =>
220221
!supportsEditorHistorySetting(state) || getRetainEditorHistory(state)
221222

223+
export const isOnCausalCluster = (state: GlobalState): boolean => {
224+
const version = getSemanticVersion(state)
225+
if (!version) return false
226+
227+
if (gte(version, VERSION_FOR_CLUSTER_ROLE_IN_SHOW_DB)) {
228+
return getDatabases(state).some(database => database.role !== 'standalone')
229+
} else {
230+
return hasProcedure(state, 'dbms.cluster.overview')
231+
}
232+
}
233+
export const getClusterRoleForDb = (state: GlobalState, activeDb: string) => {
234+
const version = getSemanticVersion(state)
235+
if (!version) return false
236+
237+
if (gte(version, VERSION_FOR_CLUSTER_ROLE_IN_SHOW_DB)) {
238+
return getDatabases(state).find(database => database.name === activeDb)
239+
?.role
240+
} else {
241+
return state[NAME].role
242+
}
243+
}
244+
222245
// Reducers
223246
const dbMetaReducer = (
224247
state = initialState,

src/shared/modules/dbMeta/epics.ts renamed to src/shared/modules/dbMeta/dbMetaEpics.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import {
3636
FORCE_FETCH,
3737
SYSTEM_DB,
3838
metaQuery,
39-
serverInfoQuery
39+
serverInfoQuery,
40+
VERSION_FOR_CLUSTER_ROLE_IN_SHOW_DB
4041
} from './dbMetaDuck'
4142
import {
4243
Database,
@@ -76,6 +77,7 @@ import {
7677
getListProcedureQuery
7778
} from '../cypher/functionsAndProceduresHelper'
7879
import { isInt, Record } from 'neo4j-driver'
80+
import { gte } from 'semver'
7981

8082
async function databaseList(store: any) {
8183
try {
@@ -191,17 +193,22 @@ async function clusterRole(store: any) {
191193
return
192194
}
193195

196+
const version = getSemanticVersion(store.getState())
197+
if (version && gte(version, VERSION_FOR_CLUSTER_ROLE_IN_SHOW_DB)) {
198+
// No need to query for the cluster role anymore since it's available in the data from SHOW DATABASES
199+
return
200+
}
201+
194202
const res = await bolt.directTransaction(
195203
getDbClusterRole(store.getState()),
196204
{},
197205
backgroundTxMetadata
198206
)
199207

200-
if (!res) return Rx.Observable.of(null)
208+
if (!res) return
201209

202210
const role = res.records[0].get(0)
203211
store.dispatch(update({ role }))
204-
return Rx.Observable.of(null)
205212
}
206213

207214
async function fetchServerInfo(store: any) {

src/shared/rootEpic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import {
5959
clearMetaOnDisconnectEpic,
6060
dbMetaEpic,
6161
serverConfigEpic
62-
} from './modules/dbMeta/epics'
62+
} from './modules/dbMeta/dbMetaEpics'
6363
import {
6464
discoveryOnStartupEpic,
6565
injectDiscoveryEpic

src/shared/utils/selectors.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import {
99
import {
1010
getAllowOutgoingConnections,
1111
getClientsAllowTelemetry,
12-
getDatabases,
13-
getRawVersion,
14-
hasProcedure,
1512
isServerConfigDone,
1613
shouldAllowOutgoingConnections
1714
} from 'shared/modules/dbMeta/dbMetaDuck'
@@ -99,14 +96,3 @@ export const getTelemetrySettings = (state: GlobalState): TelemetrySettings => {
9996

10097
return { source, ...rules[source] }
10198
}
102-
103-
export const isOnCausalCluster = (state: GlobalState): boolean => {
104-
const version = semver.coerce(getRawVersion(state))
105-
if (!version) return false
106-
107-
if (semver.gte(version, '4.3.0')) {
108-
return getDatabases(state).some(database => database.role !== 'standalone')
109-
} else {
110-
return hasProcedure(state, 'dbms.cluster.overview')
111-
}
112-
}

0 commit comments

Comments
 (0)