Repository navigation
refactor(bigtable): make sessionMap shared by permissions #14662
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,54 +15,35 @@ | |
| */ | ||
| package com.google.cloud.bigtable.data.v2.internal.compat.ops; | ||
|
|
||
| import com.google.bigtable.v2.OpenAuthorizedViewRequest; | ||
| import com.google.bigtable.v2.OpenTableRequest.Permission; | ||
| import com.google.bigtable.v2.SessionMutateRowRequest; | ||
| import com.google.cloud.bigtable.data.v2.internal.api.AuthorizedViewAsync; | ||
| import com.google.cloud.bigtable.data.v2.internal.api.Client; | ||
| import com.google.cloud.bigtable.data.v2.internal.api.TableAsync; | ||
| import com.google.cloud.bigtable.data.v2.internal.session.SessionPool; | ||
| import com.google.cloud.bigtable.data.v2.models.AuthorizedViewId; | ||
| import com.google.cloud.bigtable.data.v2.models.RowMutation; | ||
| import com.google.cloud.bigtable.data.v2.models.TableId; | ||
| import com.google.cloud.bigtable.data.v2.models.TargetId; | ||
| import io.grpc.Deadline; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| public class MutateRowShim implements UnaryShim<RowMutation, Void> { | ||
|
|
||
| private final SessionPoolMap<TableId, TableAsync> tables; | ||
| private final SessionPoolMap<AuthorizedViewId, AuthorizedViewAsync> authViews; | ||
| private final SessionPoolMap sessionMap; | ||
|
|
||
| public MutateRowShim(Client client) { | ||
| tables = | ||
| new SessionPoolMap<>( | ||
| k -> client.openTableAsync(k.getTableId(), Permission.PERMISSION_WRITE)); | ||
| authViews = | ||
| new SessionPoolMap<>( | ||
| k -> | ||
| client.openAuthorizedViewAsync( | ||
| k.getTableId(), | ||
| k.getAuthorizedViewId(), | ||
| OpenAuthorizedViewRequest.Permission.PERMISSION_WRITE)); | ||
| public MutateRowShim(SessionPoolMap sessionMap) { | ||
| this.sessionMap = sessionMap; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| tables.invalidateAll(); | ||
| authViews.invalidateAll(); | ||
| } | ||
| public void close() {} | ||
|
|
||
| @Override | ||
| public boolean supports(RowMutation request) { | ||
| TargetId targetId = request.getTargetId(); | ||
| SessionPool<?> pool; | ||
| // TODO: avoid double lookup | ||
| if (targetId instanceof TableId) { | ||
| pool = tables.get((TableId) targetId).getSessionPool(); | ||
| pool = sessionMap.get((TableId) targetId, SessionKey.Permission.WRITE).getSessionPool(); | ||
| } else if (targetId instanceof AuthorizedViewId) { | ||
| pool = authViews.get((AuthorizedViewId) targetId).getSessionPool(); | ||
| pool = | ||
| sessionMap.get((AuthorizedViewId) targetId, SessionKey.Permission.WRITE).getSessionPool(); | ||
| } else { | ||
|
Comment on lines
41
to
47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SessionPool<?> pool;
if (targetId instanceof TableId) {
com.google.cloud.bigtable.data.v2.internal.api.TableAsync table =
sessionMap.getIfPresent((TableId) targetId, SessionKey.Permission.WRITE);
if (table == null) {
return true;
}
pool = table.getSessionPool();
} else if (targetId instanceof AuthorizedViewId) {
com.google.cloud.bigtable.data.v2.internal.api.AuthorizedViewAsync view =
sessionMap.getIfPresent((AuthorizedViewId) targetId, SessionKey.Permission.WRITE);
if (view == null) {
return true;
}
pool = view.getSessionPool();
} else { |
||
| return false; | ||
| } | ||
|
|
@@ -76,12 +57,16 @@ public CompletableFuture<Void> call(RowMutation request, Deadline deadline) { | |
| SessionMutateRowRequest innerReq = request.toSessionProto(); | ||
|
|
||
| if (targetId instanceof TableId) { | ||
| return tables.apply( | ||
| (TableId) targetId, t -> t.mutateRow(innerReq, deadline).thenApply(r -> null)); | ||
| return sessionMap.apply( | ||
| (TableId) targetId, | ||
| SessionKey.Permission.WRITE, | ||
| t -> t.mutateRow(innerReq, deadline).thenApply(r -> null)); | ||
| } | ||
| if (targetId instanceof AuthorizedViewId) { | ||
| return authViews.apply( | ||
| (AuthorizedViewId) targetId, v -> v.mutateRow(innerReq, deadline).thenApply(r -> null)); | ||
| return sessionMap.apply( | ||
| (AuthorizedViewId) targetId, | ||
| SessionKey.Permission.WRITE, | ||
| v -> v.mutateRow(innerReq, deadline).thenApply(r -> null)); | ||
| } | ||
|
|
||
| CompletableFuture<Void> f = new CompletableFuture<>(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,13 +15,8 @@ | |
| */ | ||
| package com.google.cloud.bigtable.data.v2.internal.compat.ops; | ||
|
|
||
| import com.google.bigtable.v2.OpenAuthorizedViewRequest; | ||
| import com.google.bigtable.v2.OpenTableRequest.Permission; | ||
| import com.google.bigtable.v2.SessionReadModifyWriteRowRequest; | ||
| import com.google.bigtable.v2.SessionReadModifyWriteRowResponse; | ||
| import com.google.cloud.bigtable.data.v2.internal.api.AuthorizedViewAsync; | ||
| import com.google.cloud.bigtable.data.v2.internal.api.Client; | ||
| import com.google.cloud.bigtable.data.v2.internal.api.TableAsync; | ||
| import com.google.cloud.bigtable.data.v2.internal.session.SessionPool; | ||
| import com.google.cloud.bigtable.data.v2.models.AuthorizedViewId; | ||
| import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow; | ||
|
|
@@ -33,38 +28,26 @@ | |
| public class ReadModifyWriteRowShim | ||
| implements UnaryShim<ReadModifyWriteRow, SessionReadModifyWriteRowResponse> { | ||
|
|
||
| private final SessionPoolMap<TableId, TableAsync> tables; | ||
| private final SessionPoolMap<AuthorizedViewId, AuthorizedViewAsync> authViews; | ||
| private final SessionPoolMap sessionMap; | ||
|
|
||
| public ReadModifyWriteRowShim(Client client) { | ||
| // ReadModifyWriteRow reads and modifies cells and returns the row, so it needs | ||
| // read + write access on the session. | ||
| tables = | ||
| new SessionPoolMap<>( | ||
| k -> client.openTableAsync(k.getTableId(), Permission.PERMISSION_READ_WRITE)); | ||
| authViews = | ||
| new SessionPoolMap<>( | ||
| k -> | ||
| client.openAuthorizedViewAsync( | ||
| k.getTableId(), | ||
| k.getAuthorizedViewId(), | ||
| OpenAuthorizedViewRequest.Permission.PERMISSION_READ_WRITE)); | ||
| public ReadModifyWriteRowShim(SessionPoolMap sessionMap) { | ||
| this.sessionMap = sessionMap; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| tables.invalidateAll(); | ||
| authViews.invalidateAll(); | ||
| } | ||
| public void close() {} | ||
|
|
||
| @Override | ||
| public boolean supports(ReadModifyWriteRow request) { | ||
| TargetId targetId = request.getTargetId(); | ||
| SessionPool<?> pool; | ||
| if (targetId instanceof TableId) { | ||
| pool = tables.get((TableId) targetId).getSessionPool(); | ||
| pool = sessionMap.get((TableId) targetId, SessionKey.Permission.READ_WRITE).getSessionPool(); | ||
| } else if (targetId instanceof AuthorizedViewId) { | ||
| pool = authViews.get((AuthorizedViewId) targetId).getSessionPool(); | ||
| pool = | ||
| sessionMap | ||
| .get((AuthorizedViewId) targetId, SessionKey.Permission.READ_WRITE) | ||
| .getSessionPool(); | ||
| } else { | ||
|
Comment on lines
43
to
51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SessionPool<?> pool;
if (targetId instanceof TableId) {
com.google.cloud.bigtable.data.v2.internal.api.TableAsync table =
sessionMap.getIfPresent((TableId) targetId, SessionKey.Permission.READ_WRITE);
if (table == null) {
return true;
}
pool = table.getSessionPool();
} else if (targetId instanceof AuthorizedViewId) {
com.google.cloud.bigtable.data.v2.internal.api.AuthorizedViewAsync view =
sessionMap.getIfPresent((AuthorizedViewId) targetId, SessionKey.Permission.READ_WRITE);
if (view == null) {
return true;
}
pool = view.getSessionPool();
} else { |
||
| return false; | ||
| } | ||
|
|
@@ -78,11 +61,16 @@ public CompletableFuture<SessionReadModifyWriteRowResponse> call( | |
| SessionReadModifyWriteRowRequest innerReq = request.toSessionProto(); | ||
|
|
||
| if (targetId instanceof TableId) { | ||
| return tables.apply((TableId) targetId, t -> t.readModifyWriteRow(innerReq, deadline)); | ||
| return sessionMap.apply( | ||
| (TableId) targetId, | ||
| SessionKey.Permission.READ_WRITE, | ||
| t -> t.readModifyWriteRow(innerReq, deadline)); | ||
| } | ||
| if (targetId instanceof AuthorizedViewId) { | ||
| return authViews.apply( | ||
| (AuthorizedViewId) targetId, v -> v.readModifyWriteRow(innerReq, deadline)); | ||
| return sessionMap.apply( | ||
| (AuthorizedViewId) targetId, | ||
| SessionKey.Permission.READ_WRITE, | ||
| v -> v.readModifyWriteRow(innerReq, deadline)); | ||
| } | ||
|
|
||
| CompletableFuture<SessionReadModifyWriteRowResponse> f = new CompletableFuture<>(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.