Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Search
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSDU
OSDU Data Platform
System
Search
Commits
df76724d
Commit
df76724d
authored
2 years ago
by
Zhibin Mai
Browse files
Options
Downloads
Patches
Plain Diff
Optimize the performance to get the aliases for a list of kinds
parent
c850054e
No related branches found
No related tags found
1 merge request
!423
Use alias to shorten the index names in multi-kind searchMulti kind alias
Pipeline
#161942
failed
2 years ago
Stage: review
Pipeline: Search
#161943
Changes
1
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
search-core/src/main/java/org/opengroup/osdu/search/service/IndexAliasServiceImpl.java
+50
-24
50 additions, 24 deletions
.../opengroup/osdu/search/service/IndexAliasServiceImpl.java
with
50 additions
and
24 deletions
search-core/src/main/java/org/opengroup/osdu/search/service/IndexAliasServiceImpl.java
+
50
−
24
View file @
df76724d
...
...
@@ -31,10 +31,7 @@ import org.springframework.stereotype.Component;
import
javax.inject.Inject
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.*
;
import
java.util.stream.Collectors
;
@Component
...
...
@@ -51,24 +48,31 @@ public class IndexAliasServiceImpl implements IndexAliasService {
@Override
public
Map
<
String
,
String
>
getIndicesAliases
(
List
<
String
>
kinds
)
{
Map
<
String
,
String
>
aliases
=
new
HashMap
<>();
List
<
String
>
unresolvedKinds
=
new
ArrayList
<>();
List
<
String
>
validKinds
=
kinds
.
stream
().
filter
(
k
->
elasticIndexNameResolver
.
isIndexAliasSupported
(
k
)).
collect
(
Collectors
.
toList
());
for
(
String
kind:
validKinds
)
{
String
alias
=
indexAliasCache
.
get
(
kind
);
if
(!
Strings
.
isNullOrEmpty
(
alias
))
{
aliases
.
put
(
kind
,
alias
);
}
else
{
unresolvedKinds
.
add
(
kind
);
}
}
try
(
RestHighLevelClient
restClient
=
this
.
elasticClientHandler
.
createRestClient
())
{
for
(
String
kind:
kinds
)
{
String
alias
=
null
;
if
(
elasticIndexNameResolver
.
isIndexAliasSupported
(
kind
))
{
alias
=
indexAliasCache
.
get
(
kind
);
if
(
Strings
.
isNullOrEmpty
(
alias
))
{
try
{
alias
=
getOrCreateIndexAliases
(
restClient
,
kind
);
if
(!
Strings
.
isNullOrEmpty
(
alias
))
{
indexAliasCache
.
put
(
kind
,
alias
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
String
.
format
(
"Fail to get or create index alias for kind '%s'"
,
kind
),
e
);
}
}
// It is much faster to get all the aliases and verify it locally than to verify it remotely.
Set
<
String
>
allExistingAliases
=
getAllExistingAliases
(
restClient
);
for
(
String
kind:
unresolvedKinds
)
{
String
alias
=
elasticIndexNameResolver
.
getIndexAliasFromKind
(
kind
);
if
(!
allExistingAliases
.
contains
(
alias
))
{
alias
=
createIndexAlias
(
restClient
,
kind
);
}
if
(!
Strings
.
isNullOrEmpty
(
alias
))
{
aliases
.
put
(
kind
,
alias
);
indexAliasCache
.
put
(
kind
,
alias
);
}
aliases
.
put
(
kind
,
alias
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
String
.
format
(
"Fail to get or create index aliases for kinds"
),
e
);
...
...
@@ -84,7 +88,13 @@ public class IndexAliasServiceImpl implements IndexAliasService {
String
alias
=
indexAliasCache
.
get
(
kind
);
if
(
Strings
.
isNullOrEmpty
(
alias
))
{
try
(
RestHighLevelClient
restClient
=
this
.
elasticClientHandler
.
createRestClient
())
{
alias
=
getOrCreateIndexAliases
(
restClient
,
kind
);
if
(
isIndexAliasExistForKind
(
restClient
,
kind
))
{
alias
=
elasticIndexNameResolver
.
getIndexAliasFromKind
(
kind
);
}
else
{
alias
=
createIndexAlias
(
restClient
,
kind
);
}
if
(!
Strings
.
isNullOrEmpty
(
alias
))
{
indexAliasCache
.
put
(
kind
,
alias
);
}
...
...
@@ -97,14 +107,29 @@ public class IndexAliasServiceImpl implements IndexAliasService {
return
alias
;
}
private
String
getOrCreateIndexAliases
(
RestHighLevelClient
restClient
,
String
kind
)
throws
IOException
{
private
Set
<
String
>
getAllExistingAliases
(
RestHighLevelClient
restClient
)
throws
IOException
{
GetAliasesRequest
request
=
new
GetAliasesRequest
();
GetAliasesResponse
response
=
restClient
.
indices
().
getAlias
(
request
,
RequestOptions
.
DEFAULT
);
if
(
response
.
status
()
!=
RestStatus
.
OK
)
return
new
HashSet
<>();
Set
<
String
>
allAliases
=
new
HashSet
<>();
for
(
Set
<
AliasMetadata
>
aliasSet:
response
.
getAliases
().
values
())
{
List
<
String
>
aliases
=
aliasSet
.
stream
().
map
(
a
->
a
.
getAlias
()).
collect
(
Collectors
.
toList
());
allAliases
.
addAll
(
aliases
);
}
return
allAliases
;
}
private
boolean
isIndexAliasExistForKind
(
RestHighLevelClient
restClient
,
String
kind
)
throws
IOException
{
String
alias
=
elasticIndexNameResolver
.
getIndexAliasFromKind
(
kind
);
GetAliasesRequest
request
=
new
GetAliasesRequest
(
alias
);
if
(
restClient
.
indices
().
existsAlias
(
request
,
RequestOptions
.
DEFAULT
))
{
return
alias
;
}
return
(
restClient
.
indices
().
existsAlias
(
request
,
RequestOptions
.
DEFAULT
));
}
private
String
createIndexAlias
(
RestHighLevelClient
restClient
,
String
kind
)
throws
IOException
{
String
index
=
elasticIndexNameResolver
.
getIndexNameFromKind
(
kind
);
String
alias
=
elasticIndexNameResolver
.
getIndexAliasFromKind
(
kind
);
// To create an alias for an index, the index name must the concrete index name, not alias
index
=
resolveConcreteIndexName
(
restClient
,
index
);
if
(!
Strings
.
isNullOrEmpty
(
index
))
{
...
...
@@ -122,6 +147,7 @@ public class IndexAliasServiceImpl implements IndexAliasService {
return
null
;
}
private
String
resolveConcreteIndexName
(
RestHighLevelClient
restClient
,
String
index
)
throws
IOException
{
GetAliasesRequest
request
=
new
GetAliasesRequest
(
index
);
GetAliasesResponse
response
=
restClient
.
indices
().
getAlias
(
request
,
RequestOptions
.
DEFAULT
);
...
...
This diff is collapsed.
Click to expand it.
Zhibin Mai
@zmai
mentioned in commit
da44d51e
·
2 years ago
mentioned in commit
da44d51e
mentioned in commit da44d51ec12f0d5880be20f07ca53e23fa50626b
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment