@@ -60,80 +60,88 @@ func NewGCDatastoreCommand(programName string, cfg *datastore.Config) *cobra.Com
6060 Long : "Executes garbage collection against the datastore. Deletes stale relationships, expired relationships, and stale transactions." ,
6161 PreRunE : server .DefaultPreRunE (programName ),
6262 RunE : termination .PublishError (func (cmd * cobra.Command , args []string ) error {
63- ctx := context .Background ()
64-
65- // Disable background GC and hedging.
66- cfg .GCInterval = - 1 * time .Hour
67- cfg .RequestHedgingEnabled = false
68-
69- ds , err := datastore .NewDatastore (ctx , cfg .ToOption ())
70- if err != nil {
71- return fmt .Errorf ("failed to create datastore: %w" , err )
72- }
73-
74- gcds := dspkg.UnwrapAs [common.GarbageCollectableDatastore ](ds )
75- if gcds == nil {
76- return fmt .Errorf ("datastore of type %T does not support garbage collection" , ds )
77- }
78-
79- log .Ctx (ctx ).Info ().
80- Float64 ("gc_window_seconds" , cfg .GCWindow .Seconds ()).
81- Float64 ("gc_max_operation_time_seconds" , cfg .GCMaxOperationTime .Seconds ()).
82- Msg ("Running garbage collection..." )
83-
84- err = common .RunGarbageCollection (gcds , cfg .GCWindow , cfg .GCMaxOperationTime )
85- if err != nil {
86- return err
87- }
88-
89- log .Ctx (ctx ).Info ().Msg ("Garbage collection completed" )
90- return nil
63+ return executeGC (cfg )
9164 }),
9265 }
9366}
9467
68+ func executeGC (cfg * datastore.Config ) error {
69+ ctx := context .Background ()
70+
71+ // Disable background GC and hedging.
72+ cfg .GCInterval = - 1 * time .Hour
73+ cfg .RequestHedgingEnabled = false
74+
75+ ds , err := datastore .NewDatastore (ctx , cfg .ToOption ())
76+ if err != nil {
77+ return fmt .Errorf ("failed to create datastore: %w" , err )
78+ }
79+
80+ gcds := dspkg.UnwrapAs [common.GarbageCollectableDatastore ](ds )
81+ if gcds == nil {
82+ return fmt .Errorf ("datastore of type '%s' does not support garbage collection" , cfg .Engine )
83+ }
84+
85+ log .Ctx (ctx ).Info ().
86+ Float64 ("gc_window_seconds" , cfg .GCWindow .Seconds ()).
87+ Float64 ("gc_max_operation_time_seconds" , cfg .GCMaxOperationTime .Seconds ()).
88+ Msg ("Running garbage collection..." )
89+
90+ err = common .RunGarbageCollection (gcds , cfg .GCWindow , cfg .GCMaxOperationTime )
91+ if err != nil {
92+ return err
93+ }
94+
95+ log .Ctx (ctx ).Info ().Msg ("Garbage collection completed" )
96+ return nil
97+ }
98+
9599func NewRepairDatastoreCommand (programName string , cfg * datastore.Config ) * cobra.Command {
96100 return & cobra.Command {
97101 Use : "repair" ,
98102 Short : "executes datastore repair" ,
99103 Long : "Executes a repair operation for the datastore" ,
100104 PreRunE : server .DefaultPreRunE (programName ),
101105 RunE : termination .PublishError (func (cmd * cobra.Command , args []string ) error {
102- ctx := context .Background ()
103-
104- // Disable background GC and hedging.
105- cfg .GCInterval = - 1 * time .Hour
106- cfg .RequestHedgingEnabled = false
107-
108- ds , err := datastore .NewDatastore (ctx , cfg .ToOption ())
109- if err != nil {
110- return fmt .Errorf ("failed to create datastore: %w" , err )
111- }
112-
113- repairable := dspkg.UnwrapAs [dspkg.RepairableDatastore ](ds )
114- if repairable == nil {
115- return fmt .Errorf ("datastore of type %T does not support the repair operation" , ds )
116- }
117-
118- if len (args ) == 0 {
119- fmt .Println ()
120- fmt .Println ("Available repair operations:" )
121- for _ , op := range repairable .RepairOperations () {
122- fmt .Printf ("\t %s: %s\n " , op .Name , op .Description )
123- }
124- return nil
125- }
126-
127- operationName := args [0 ]
128-
129- log .Ctx (ctx ).Info ().Msg ("Running repair..." )
130- err = repairable .Repair (ctx , operationName , true )
131- if err != nil {
132- return err
133- }
134-
135- log .Ctx (ctx ).Info ().Msg ("Datastore repair completed" )
136- return nil
106+ return executeRepair (cfg , args )
137107 }),
138108 }
139109}
110+
111+ func executeRepair (cfg * datastore.Config , args []string ) error {
112+ ctx := context .Background ()
113+
114+ // Disable background GC and hedging.
115+ cfg .GCInterval = - 1 * time .Hour
116+ cfg .RequestHedgingEnabled = false
117+
118+ ds , err := datastore .NewDatastore (ctx , cfg .ToOption ())
119+ if err != nil {
120+ return fmt .Errorf ("failed to create datastore: %w" , err )
121+ }
122+
123+ repairable := dspkg.UnwrapAs [dspkg.RepairableDatastore ](ds )
124+ if repairable == nil {
125+ return fmt .Errorf ("datastore of type '%s' does not support the repair operation" , cfg .Engine )
126+ }
127+
128+ if len (args ) == 0 {
129+ fmt .Println ()
130+ fmt .Println ("Available repair operations:" )
131+ for _ , op := range repairable .RepairOperations () {
132+ fmt .Printf ("\t %s: %s\n " , op .Name , op .Description )
133+ }
134+ return nil
135+ }
136+
137+ operationName := args [0 ]
138+
139+ log .Ctx (ctx ).Info ().Msg ("Running repair..." )
140+ err = repairable .Repair (ctx , operationName , true )
141+ if err != nil {
142+ return err
143+ }
144+
145+ log .Ctx (ctx ).Info ().Msg ("Datastore repair completed" )
146+ return nil
147+ }
0 commit comments