Skip to content
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

Fixup metadata dump with table_exists_action #303

Merged
merged 1 commit into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions oracle/controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,6 @@ func (d *GRPCDatabaseClientFactory) New(ctx context.Context, r client.Reader, na
return dbdpb.NewDatabaseDaemonClient(conn), conn.Close, nil
}

// Contains check whether given "elem" presents in "array"
func Contains(array []string, elem string) bool {
for _, v := range array {
if v == elem {
return true
}
}
return false
}

// Filter Returns a slice that doesn't contain element
func Filter(slice []string, element string) []string {
//This implementation isn't the fastest, but it protects against slices containing a single element.
result := make([]string, 0, len(slice))
for _, s := range slice {
if s != element {
result = append(result, s)
}
}
return result
}

// GetBackupGcsPath resolves the actual gcs path based on backup spec.
func GetBackupGcsPath(backup *v1alpha1.Backup) string {
gcsPath := backup.Spec.GcsPath
Expand Down
1 change: 1 addition & 0 deletions oracle/controllers/databasecontroller/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//oracle/controllers",
"//oracle/pkg/agents/common/sql",
"//oracle/pkg/k8s",
"//oracle/pkg/util",
"@com_github_go_logr_logr//:logr",
"@io_k8s_api//core/v1:core",
"@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
Expand Down
7 changes: 4 additions & 3 deletions oracle/controllers/databasecontroller/database_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/GoogleCloudPlatform/elcarro-oracle-operator/oracle/controllers"
"github.com/GoogleCloudPlatform/elcarro-oracle-operator/oracle/pkg/agents/common/sql"
"github.com/GoogleCloudPlatform/elcarro-oracle-operator/oracle/pkg/k8s"
"github.com/GoogleCloudPlatform/elcarro-oracle-operator/oracle/pkg/util"
)

const (
Expand Down Expand Up @@ -151,8 +152,8 @@ func (r *DatabaseReconciler) ReconcileDatabaseDeletion(ctx context.Context, req
}

// Remove PDB from list of DatabaseNames
if controllers.Contains(inst.Status.DatabaseNames, db.Spec.Name) {
inst.Status.DatabaseNames = controllers.Filter(inst.Status.DatabaseNames, db.Spec.Name)
if util.Contains(inst.Status.DatabaseNames, db.Spec.Name) {
inst.Status.DatabaseNames = util.Filter(inst.Status.DatabaseNames, db.Spec.Name)
}
if err := r.Status().Update(ctx, &inst); err != nil {
log.Error(err, "failed to update the Instance status after deleting a Database(PDB)", "DatabaseName", db.Name, "InstanceName", inst.Name)
Expand Down Expand Up @@ -267,7 +268,7 @@ func (r *DatabaseReconciler) ReconcileDatabaseCreation(ctx context.Context, req
}

// check DB name against existing ones to decide whether this is a new DB
if !controllers.Contains(inst.Status.DatabaseNames, db.Spec.Name) {
if !util.Contains(inst.Status.DatabaseNames, db.Spec.Name) {
log.Info("found a new DB", "dbName", db.Spec.Name)
inst.Status.DatabaseNames = append(inst.Status.DatabaseNames, db.Spec.Name)
} else {
Expand Down
1 change: 1 addition & 0 deletions oracle/pkg/database/dbdaemon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//oracle/pkg/agents/common",
"//oracle/pkg/agents/common/sql",
"//oracle/pkg/agents/consts",
"//oracle/pkg/agents/oracle",
"//oracle/pkg/agents/pitr",
Expand Down
46 changes: 40 additions & 6 deletions oracle/pkg/database/dbdaemon/dbdaemon_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"k8s.io/klog/v2"

"github.com/GoogleCloudPlatform/elcarro-oracle-operator/oracle/pkg/agents/common"
sqlq "github.com/GoogleCloudPlatform/elcarro-oracle-operator/oracle/pkg/agents/common/sql"
"github.com/GoogleCloudPlatform/elcarro-oracle-operator/oracle/pkg/agents/consts"
dbdpb "github.com/GoogleCloudPlatform/elcarro-oracle-operator/oracle/pkg/agents/oracle"
"github.com/GoogleCloudPlatform/elcarro-oracle-operator/oracle/pkg/agents/pitr"
Expand Down Expand Up @@ -493,6 +494,30 @@ func (s *Server) PhysicalRestoreAsync(ctx context.Context, req *dbdpb.PhysicalRe
return &lropb.Operation{Name: job.ID(), Done: false}, nil
}

func filterParamsForMetadata(in []string) []string {
// These parameters are incompatible with sqlfile, so we must remove
// them for our metadata dump.
restricted := []string{
"TABLE_EXISTS_ACTION",
}

var filtered []string
for _, i := range in {
safe := true
for _, r := range restricted {
if strings.Split(i, "=")[0] == r {
safe = false
break
}
}
if safe {
filtered = append(filtered, i)
}
}

return filtered
}

// dataPumpImport runs impdp Oracle tool against existing PDB which
// imports data from a data pump .dmp file.
func (s *Server) dataPumpImport(ctx context.Context, req *dbdpb.DataPumpImportRequest) (*dbdpb.DataPumpImportResponse, error) {
Expand Down Expand Up @@ -533,7 +558,7 @@ func (s *Server) dataPumpImport(ctx context.Context, req *dbdpb.DataPumpImportRe
// impdp \"/ as sysdba\" sqlfile=test_meta_tables.sql dumpfile=prod_export_cmms02072022.dmp directory=REFRESH_DUMP_DIR NOLOGFILE=YES FULL=Y INCLUDE=TABLESPACE INCLUDE=TABLESPACE_QUOTA INCLUDE=USER PARALLEL=4
// We dont dump tables because this can be slow O(minutes), but it would be more precise if needed it can be added.
tsCheckParams := []string{impdpTarget}
tsCheckParams = append(tsCheckParams, req.CommandParams...)
tsCheckParams = append(tsCheckParams, filterParamsForMetadata(req.CommandParams)...)
tsCheckParams = append(tsCheckParams, fmt.Sprintf("directory=%s", consts.DpdumpDir.Oracle))
tsCheckParams = append(tsCheckParams, "dumpfile="+importFilename)
tsCheckParams = append(tsCheckParams, "sqlfile="+importMetaFile)
Expand All @@ -555,7 +580,7 @@ func (s *Server) dataPumpImport(ctx context.Context, req *dbdpb.DataPumpImportRe
}

metaFullPath := filepath.Join(dumpDir, importMetaFile)
s.createTablespacesFromSqlfile(ctx, metaFullPath)
s.createTablespacesFromSqlfile(ctx, metaFullPath, req.PdbName)

params := []string{impdpTarget}
params = append(params, req.CommandParams...)
Expand Down Expand Up @@ -594,7 +619,7 @@ var tsRegexp = regexp.MustCompile("(DEFAULT|CREATE|UNDO|TEMPORARY) TABLESPACE \"
// references. It gathers these references and then ensures the tablespaces are
// created as BIGFILE for regular tablespaces or as AUTOEXTEND single datafile
// for temporary tablespaces.
func (s *Server) createTablespacesFromSqlfile(ctx context.Context, metaFullPath string) {
func (s *Server) createTablespacesFromSqlfile(ctx context.Context, metaFullPath, PDBName string) {
f, err := os.Open(metaFullPath)
if err != nil {
klog.Warningf("Not creating tablespaces for import. Failed to open %q: %v", metaFullPath, err)
Expand Down Expand Up @@ -630,7 +655,10 @@ func (s *Server) createTablespacesFromSqlfile(ctx context.Context, metaFullPath
}

sqlResp, err := s.RunSQLPlusFormatted(ctx, &dbdpb.RunSQLPlusCMDRequest{
Commands: []string{"select contents, tablespace_name from dba_tablespaces"},
Commands: []string{
sqlq.QuerySetSessionContainer(PDBName),
"select contents, tablespace_name from dba_tablespaces",
},
})
if err != nil {
klog.Warningf("createTablespacesFromSqlfile: query tablespaces failed: %v", err)
Expand Down Expand Up @@ -658,15 +686,21 @@ func (s *Server) createTablespacesFromSqlfile(ctx context.Context, metaFullPath
// Create any remaining tablespaces
for t := range ts {
_, err := s.RunSQLPlus(ctx, &dbdpb.RunSQLPlusCMDRequest{
Commands: []string{"create bigfile tablespace \"%s\""},
Commands: []string{
sqlq.QuerySetSessionContainer(PDBName),
fmt.Sprintf("create bigfile tablespace \"%s\"", t),
},
})
if err != nil {
klog.Warningf("createTablespacesFromSqlfile: failed to create tablespace %s: %v", t, err)
}
}
for t := range tsTemp {
_, err := s.RunSQLPlus(ctx, &dbdpb.RunSQLPlusCMDRequest{
Commands: []string{"create temporary tablespace \"%s\" size 128M autoextend on"},
Commands: []string{
sqlq.QuerySetSessionContainer(PDBName),
fmt.Sprintf("create temporary tablespace \"%s\" size 128M autoextend on", t),
},
})
if err != nil {
klog.Warningf("createTablespacesFromSqlfile: failed to create tablespace %s: %v", t, err)
Expand Down
22 changes: 22 additions & 0 deletions oracle/pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,25 @@ func (g *GCSUtilImpl) Delete(ctx context.Context, gcsPath string) error {
}
return nil
}

// Contains check whether given "elem" presents in "array"
func Contains(array []string, elem string) bool {
for _, v := range array {
if v == elem {
return true
}
}
return false
}

// Filter Returns a slice that doesn't contain element
func Filter(slice []string, element string) []string {
//This implementation isn't the fastest, but it protects against slices containing a single element.
result := make([]string, 0, len(slice))
for _, s := range slice {
if s != element {
result = append(result, s)
}
}
return result
}