Skip to content

Commit

Permalink
add SpannerMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Nov 21, 2024
1 parent e143fd7 commit f868311
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 228 deletions.
26 changes: 15 additions & 11 deletions Spanner/src/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ public function create(
}

$request = $this->serializer->decodeMessage(new CreateBackupRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->instance->name());

return $this->databaseAdminClient->createBackup($request, $callOptions)
return $this->databaseAdminClient->createBackup($request, $callOptions + [
'resource-prefix' => $this->instance->name(),
])
->withResultFunction($this->backupResultFunction());
}

Expand Down Expand Up @@ -168,9 +168,10 @@ public function createCopy(
];

$request = $this->serializer->decodeMessage(new CopyBackupRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->instance->name());

return $this->databaseAdminClient->copyBackup($request, $callOptions)
return $this->databaseAdminClient->copyBackup($request, $callOptions + [
'resource-prefix' => $this->instance->name(),
])
->withResultFunction($this->backupResultFunction());
}

Expand All @@ -193,9 +194,10 @@ public function delete(array $options = []): void
];

$request = $this->serializer->decodeMessage(new DeleteBackupRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

$this->databaseAdminClient->deleteBackup($request, $callOptions);
$this->databaseAdminClient->deleteBackup($request, $callOptions + [
'resource-prefix' => $this->name,
]);
}

/**
Expand Down Expand Up @@ -277,9 +279,10 @@ public function reload(array $options = []): array
];

$request = $this->serializer->decodeMessage(new GetBackupRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

$response = $this->databaseAdminClient->getBackup($request, $callOptions);
$response = $this->databaseAdminClient->getBackup($request, $callOptions + [
'resource-prefix' => $this->name,
]);
return $this->info = $this->handleResponse($response);
}

Expand Down Expand Up @@ -340,9 +343,10 @@ public function updateExpireTime(DateTimeInterface $newTimestamp, array $options
];

$request = $this->serializer->decodeMessage(new UpdateBackupRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

$response = $this->databaseAdminClient->updateBackup($request, $callOptions);
$response = $this->databaseAdminClient->updateBackup($request, $callOptions + [
'resource-prefix' => $this->name,
]);
return $this->info = $this->handleResponse($response);
}

Expand Down
81 changes: 42 additions & 39 deletions Spanner/src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,10 @@ public function reload(array $options = []): array
$data['name'] = $this->name;

$request = $this->serializer->decodeMessage(new GetDatabaseRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

$response = $this->databaseAdminClient->getDatabase($request, $callOptions);
$response = $this->databaseAdminClient->getDatabase($request, $callOptions + [
'resource-prefix' => $this->name,
]);
return $this->info = $this->handleResponse($response);
}

Expand Down Expand Up @@ -423,9 +424,9 @@ public function create(array $options = []): OperationResponse
];

$request = $this->serializer->decodeMessage(new CreateDatabaseRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->instance->name());

return $this->databaseAdminClient->createDatabase($request, $callOptions)
return $this->databaseAdminClient->createDatabase($request, $callOptions + [
'resource-prefix' => $this->instance->name(),
])
->withResultFunction($this->databaseResultFunction());
}

Expand Down Expand Up @@ -488,9 +489,10 @@ public function updateDatabase(array $options = []): OperationResponse
];

$request = $this->serializer->decodeMessage(new UpdateDatabaseRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

return $this->databaseAdminClient->updateDatabase($request, $callOptions)
return $this->databaseAdminClient->updateDatabase($request, $callOptions + [
'resource-prefix' => $this->name,
])
->withResultFunction($this->databaseResultFunction());
}

Expand Down Expand Up @@ -563,9 +565,9 @@ public function updateDdlBatch(array $statements, array $options = []): Operatio
];

$request = $this->serializer->decodeMessage(new UpdateDatabaseDdlRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

return $this->databaseAdminClient->updateDatabaseDdl($request, $callOptions);
return $this->databaseAdminClient->updateDatabaseDdl($request, $callOptions + [
'resource-prefix' => $this->name
]);
}

/**
Expand Down Expand Up @@ -596,9 +598,10 @@ public function drop(array $options = []): void
$data['database'] = $this->name;

$request = $this->serializer->decodeMessage(new DropDatabaseRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

$this->databaseAdminClient->dropDatabase($request, $callOptions);
$this->databaseAdminClient->dropDatabase($request, $callOptions + [
'resource-prefix' => $this->name
]);

if ($this->sessionPool) {
$this->sessionPool->clear();
Expand Down Expand Up @@ -633,9 +636,10 @@ public function ddl(array $options = []): array
$data['database'] = $this->name;

$request = $this->serializer->decodeMessage(new GetDatabaseDdlRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

$response = $this->databaseAdminClient->getDatabaseDdl($request, $callOptions);
$response = $this->databaseAdminClient->getDatabaseDdl($request, $callOptions + [
'resource-prefix' => $this->name
]);
$ddl = $this->handleResponse($response);

if (isset($ddl['statements'])) {
Expand Down Expand Up @@ -1683,7 +1687,6 @@ public function execute($sql, array $options = []): Result
$options['transaction'],
$options['transactionContext']
) = $this->transactionSelector($options);
$options = $this->addLarHeader($options, true, $options['transactionContext']);

$options['directedReadOptions'] = $this->configureDirectedReadOptions(
$options,
Expand All @@ -1693,7 +1696,9 @@ public function execute($sql, array $options = []): Result
try {
// Unset the internal flag.
unset($options['singleUse']);
return $this->operation->execute($session, $sql, $options);
return $this->operation->execute($session, $sql, $options + [
'route-to-leader' => $options['transactionContext'] === SessionPoolInterface::CONTEXT_READWRITE
]);
} finally {
$session->setExpiration();
}
Expand Down Expand Up @@ -1782,10 +1787,11 @@ public function batchWrite(array $mutationGroups, array $options = []): \Generat
];

$request = $this->serializer->decodeMessage(new BatchWriteRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);
$callOptions = $this->addLarHeader($callOptions, $this->routeToLeader);

$response = $this->spannerClient->batchWrite($request, $callOptions);
$response = $this->spannerClient->batchWrite($request, $callOptions + [
'resource-prefix' => $this->name,
'route-to-leader' => $this->routeToLeader,
]);
return $this->handleResponse($response);
} finally {
$this->isRunningTransaction = false;
Expand Down Expand Up @@ -1927,11 +1933,10 @@ public function executePartitionedUpdate($statement, array $options = []): int
}
$transaction = $this->operation->transaction($session, $beginTransactionOptions);

$options = $this->addLarHeader($options);

try {
return $this->operation->executeUpdate($session, $transaction, $statement, [
'statsItem' => 'rowCountLowerBound'
'statsItem' => 'rowCountLowerBound',
'route-to-leader' => true,
] + $options);
} finally {
$session->setExpiration();
Expand Down Expand Up @@ -2072,12 +2077,12 @@ public function read($table, KeySet $keySet, array $columns, array $options = []
$this->directedReadOptions ?? []
);

$options = $this->addLarHeader($options, true, $context);

try {
// Unset the internal flag.
unset($options['singleUse']);
return $this->operation->read($session, $table, $keySet, $columns, $options);
return $this->operation->read($session, $table, $keySet, $columns, $options + [
'route-to-leader' => $context === SessionPoolInterface::CONTEXT_READ
]);
} finally {
$session->setExpiration();
}
Expand Down Expand Up @@ -2203,10 +2208,10 @@ public function batchCreateSessions(array $options): array
$data['database'] = $this->name;

$request = $this->serializer->decodeMessage(new BatchCreateSessionsRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);
$callOptions = $this->addLarHeader($callOptions, $this->routeToLeader);

$response = $this->spannerClient->batchCreateSessions($request, $callOptions);
$response = $this->spannerClient->batchCreateSessions($request, $callOptions + [
'resource-prefix' => $this->name,
'route-to-leader' => $this->routeToLeader
]);
return $this->handleResponse($response);
}

Expand All @@ -2225,9 +2230,9 @@ public function deleteSessionAsync(array $options): PromiseInterface
[$data, $callOptions] = $this->splitOptionalArgs($options);

$request = $this->serializer->decodeMessage(new DeleteSessionRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

return $this->spannerClient->deleteSessionAsync($request, $callOptions);
return $this->spannerClient->deleteSessionAsync($request, $callOptions + [
'resource-prefix' => $this->name
]);
}

/**
Expand All @@ -2254,14 +2259,13 @@ public function deleteSessionAsync(array $options): PromiseInterface
public function backupOperations(array $options = []): ItemIterator
{
[$data, $callOptions] = $this->splitOptionalArgs($options);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);
$request = $this->serializer->decodeMessage(new ListBackupOperationsRequest(), $data);
$request->setParent($this->instance->name());

return $this->buildLongRunningIterator(
[$this->databaseAdminClient, 'listBackupOperations'],
$request,
$callOptions
$callOptions + ['resource-prefix' => $this->name]
);
}

Expand All @@ -2286,9 +2290,9 @@ public function createDatabaseFromBackup($name, $backup, array $options = []): O
];

$request = $this->serializer->decodeMessage(new RestoreDatabaseRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

return $this->databaseAdminClient->restoreDatabase($request, $callOptions)
return $this->databaseAdminClient->restoreDatabase($request, $callOptions + [
'resource-prefix' => $this->name
])
->withResultFunction($this->databaseResultFunction());
}

Expand Down Expand Up @@ -2316,14 +2320,13 @@ public function createDatabaseFromBackup($name, $backup, array $options = []): O
public function databaseOperations(array $options = []): ItemIterator
{
[$data, $callOptions] = $this->splitOptionalArgs($options);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);
$request = $this->serializer->decodeMessage(new ListDatabaseOperationsRequest(), $data);
$request->setParent($this->instance->name());

return $this->buildLongRunningIterator(
[$this->databaseAdminClient, 'listDatabaseOperations'],
$request,
$callOptions
$callOptions + ['resource-prefix' => $this->name]
);
}

Expand Down
31 changes: 17 additions & 14 deletions Spanner/src/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ public function exists(array $options = []): bool
'fieldMask' => ['paths' => ['name']],
];
$request = $this->serializer->decodeMessage(new GetInstanceRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->projectName);

$this->instanceAdminClient->getInstance($request, $callOptions);
$this->instanceAdminClient->getInstance($request, $callOptions + [
'resource-prefix' => $this->projectName
]);
} else {
$this->reload($options);
}
Expand Down Expand Up @@ -252,9 +253,10 @@ public function reload(array $options = []): array
}

$request = $this->serializer->decodeMessage(new GetInstanceRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->projectName);

$response = $this->instanceAdminClient->getInstance($request, $callOptions);
$response = $this->instanceAdminClient->getInstance($request, $callOptions + [
'resource-prefix' => $this->projectName
]);
return $this->info = $this->handleResponse($response);
}

Expand Down Expand Up @@ -303,9 +305,10 @@ public function create(InstanceConfiguration $config, array $options = []): Oper
];

$request = $this->serializer->decodeMessage(new CreateInstanceRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

return $this->instanceAdminClient->createInstance($request, $callOptions)
return $this->instanceAdminClient->createInstance($request, $callOptions + [
'resource-prefix' => $this->name
])
->withResultFunction($this->instanceResultFunction());
}

Expand Down Expand Up @@ -382,9 +385,10 @@ public function update(array $options = []): OperationResponse
];

$request = $this->serializer->decodeMessage(new UpdateInstanceRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

return $this->instanceAdminClient->updateInstance($request, $callOptions)
return $this->instanceAdminClient->updateInstance($request, $callOptions + [
'resource-prefix' => $this->name
])
->withResultFunction($this->instanceResultFunction());
}

Expand All @@ -409,9 +413,10 @@ public function delete(array $options = []): void
$data['name'] = $this->name;

$request = $this->serializer->decodeMessage(new DeleteInstanceRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

$this->instanceAdminClient->deleteInstance($request, $callOptions);
$this->instanceAdminClient->deleteInstance($request, $callOptions + [
'resource-prefix' => $this->name
]);
}

/**
Expand Down Expand Up @@ -539,12 +544,11 @@ public function databases(array $options = []): ItemIterator
$data['parent'] = $this->name;

$request = $this->serializer->decodeMessage(new ListDatabasesRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

return $this->buildListItemsIterator(
[$this->databaseAdminClient, 'listDatabases'],
$request,
$callOptions,
$callOptions + ['resource-prefix' => $this->name],
function (array $database) {
return $this->database($database['name'], ['database' => $database]);
},
Expand Down Expand Up @@ -614,12 +618,11 @@ public function backups(array $options = []): ItemIterator
$data['parent'] = $this->name;

$request = $this->serializer->decodeMessage(new ListBackupsRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

return $this->buildListItemsIterator(
[$this->databaseAdminClient, 'listBackups'],
$request,
$callOptions,
$callOptions + ['resource-prefix' => $this->name],
function (array $backup) {
return $this->backup($backup['name'], $backup);
},
Expand Down
Loading

0 comments on commit f868311

Please sign in to comment.