Skip to content

Commit

Permalink
refactor: log deleted URL mappings (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
boecklim authored Feb 20, 2025
1 parent 595c61b commit 17a34f5
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 862 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ install:

.PHONY: install_gen
install_gen:
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.34.2
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.36.5
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.5.1
go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@v2.3.0
go install github.com/matryer/moq@v0.4.0
Expand Down
398 changes: 108 additions & 290 deletions internal/blocktx/blocktx_api/blocktx_api.pb.go

Large diffs are not rendered by default.

300 changes: 137 additions & 163 deletions internal/callbacker/callbacker_api/callbacker_api.pb.go

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion internal/callbacker/callbacker_api/callbacker_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "google/protobuf/empty.proto";
service CallbackerAPI {
rpc Health (google.protobuf.Empty) returns (HealthResponse) {}
rpc SendCallback (SendCallbackRequest) returns (google.protobuf.Empty) {}
rpc DeleteURLMapping (DeleteURLMappingRequest) returns (google.protobuf.Empty) {}
rpc DeleteURLMapping (DeleteURLMappingRequest) returns (DeleteURLMappingResponse) {}
}

// Note: Values of the statuses have a difference between them in case
Expand Down Expand Up @@ -38,6 +38,11 @@ message HealthResponse {
google.protobuf.Timestamp timestamp = 1;
}

// swagger:model DeleteURLMappingResponse
message DeleteURLMappingResponse {
int64 rows = 1;
}

// swagger:model SendRequest
message SendRequest {
CallbackRouting callback_routing = 1;
Expand Down
10 changes: 5 additions & 5 deletions internal/callbacker/callbacker_api/callbacker_api_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/callbacker/mocks/callbacker_api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/callbacker/mocks/processor_store_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/callbacker/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ func toCallback(dto *store.CallbackData) *Callback {
}

func (p *Processor) GracefulStop() {
err := p.store.DeleteURLMapping(p.ctx, p.hostName)
rowsAffected, err := p.store.DeleteURLMapping(p.ctx, p.hostName)
if err != nil {
p.logger.Error("failed to delete URL mapping", slog.String("err", err.Error()))
p.logger.Error("Failed to delete URL mapping", slog.String("err", err.Error()))
} else {
p.logger.Info("Deleted URL mapping", slog.String("hostname", p.hostName), slog.Int64("rows", rowsAffected))
}

p.cancelAll()
Expand Down
20 changes: 12 additions & 8 deletions internal/callbacker/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ func TestProcessorStart(t *testing.T) {
DispatchFunc: func(_ string, _ *callbacker.CallbackEntry) {},
}
processorStore := &mocks.ProcessorStoreMock{
SetURLMappingFunc: func(_ context.Context, _ store.URLMapping) error { return tc.setURLMappingErr },
DeleteURLMappingFunc: func(_ context.Context, _ string) error { return nil },
SetURLMappingFunc: func(_ context.Context, _ store.URLMapping) error { return tc.setURLMappingErr },
DeleteURLMappingFunc: func(_ context.Context, _ string) (int64, error) {
return 0, nil
},
GetURLMappingsFunc: func(_ context.Context) (map[string]string, error) {
return tc.mappings, nil
},
Expand Down Expand Up @@ -278,10 +280,12 @@ func TestDispatchPersistedCallbacks(t *testing.T) {
ShutdownFunc: func() {},
}
processorStore := &mocks.ProcessorStoreMock{
SetURLMappingFunc: func(_ context.Context, _ store.URLMapping) error { return tc.setURLMappingErr },
DeleteURLMappingFunc: func(_ context.Context, _ string) error { return nil },
GetURLMappingsFunc: func(_ context.Context) (map[string]string, error) { return nil, nil },
GetUnmappedURLFunc: func(_ context.Context) (string, error) { return "https://abcdefg.com", nil },
SetURLMappingFunc: func(_ context.Context, _ store.URLMapping) error { return tc.setURLMappingErr },
DeleteURLMappingFunc: func(_ context.Context, _ string) (int64, error) {
return 0, nil
},
GetURLMappingsFunc: func(_ context.Context) (map[string]string, error) { return nil, nil },
GetUnmappedURLFunc: func(_ context.Context) (string, error) { return "https://abcdefg.com", nil },
GetAndDeleteFunc: func(_ context.Context, _ string, _ int) ([]*store.CallbackData, error) {
return tc.storedCallbacks, tc.getAndDeleteErr
},
Expand Down Expand Up @@ -325,8 +329,8 @@ func TestStartCallbackStoreCleanup(t *testing.T) {
DeleteOlderThanFunc: func(_ context.Context, _ time.Time) error {
return tc.deleteFailedOlderThanErr
},
DeleteURLMappingFunc: func(_ context.Context, _ string) error {
return nil
DeleteURLMappingFunc: func(_ context.Context, _ string) (int64, error) {
return 0, nil
},
}
dispatcher := &mocks.DispatcherMock{}
Expand Down
9 changes: 7 additions & 2 deletions internal/callbacker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ func (s *Server) SendCallback(_ context.Context, request *callbacker_api.SendCal
return nil, nil
}

func (s *Server) DeleteURLMapping(_ context.Context, request *callbacker_api.DeleteURLMappingRequest) (*emptypb.Empty, error) {
return nil, s.store.DeleteURLMapping(context.Background(), request.Instance)
func (s *Server) DeleteURLMapping(_ context.Context, request *callbacker_api.DeleteURLMappingRequest) (*callbacker_api.DeleteURLMappingResponse, error) {
rowsAffected, err := s.store.DeleteURLMapping(context.Background(), request.Instance)
if err != nil {
return nil, err
}

return &callbacker_api.DeleteURLMappingResponse{Rows: rowsAffected}, nil
}

func toCallbackDto(r *callbacker_api.SendCallbackRequest) *Callback {
Expand Down
13 changes: 9 additions & 4 deletions internal/callbacker/store/postgresql/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,21 @@ func (p *PostgreSQL) GetUnmappedURL(ctx context.Context) (url string, err error)
return url, nil
}

func (p *PostgreSQL) DeleteURLMapping(ctx context.Context, instance string) error {
func (p *PostgreSQL) DeleteURLMapping(ctx context.Context, instance string) (rowsAffected int64, err error) {
const q = `DELETE FROM callbacker.url_mapping
WHERE instance=$1`

_, err := p.db.ExecContext(ctx, q, instance)
rows, err := p.db.ExecContext(ctx, q, instance)
if err != nil {
return store.ErrURLMappingDeleteFailed
return 0, store.ErrURLMappingDeleteFailed
}

return nil
rowsAffected, err = rows.RowsAffected()
if err != nil {
return 0, nil
}

return rowsAffected, nil
}

func (p *PostgreSQL) GetURLMappings(ctx context.Context) (map[string]string, error) {
Expand Down
3 changes: 2 additions & 1 deletion internal/callbacker/store/postgresql/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ func TestPostgresDBt(t *testing.T) {
require.NoError(t, err)

// when
err = postgresDB.DeleteURLMapping(ctx, "host1")
rowsAffected, err := postgresDB.DeleteURLMapping(ctx, "host1")
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)

// then
mappings, err := postgresDB.GetURLMappings(ctx)
Expand Down
4 changes: 2 additions & 2 deletions internal/callbacker/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ type CallbackData struct {
type ProcessorStore interface {
SetURLMapping(ctx context.Context, m URLMapping) error
GetURLMappings(ctx context.Context) (urlInstanceMappings map[string]string, err error)
DeleteURLMapping(ctx context.Context, instance string) error
DeleteURLMapping(ctx context.Context, instance string) (rowsAffected int64, err error)
GetUnmappedURL(ctx context.Context) (url string, err error)
GetAndDelete(ctx context.Context, url string, limit int) ([]*CallbackData, error)
DeleteOlderThan(ctx context.Context, t time.Time) error
}

type CallbackStore interface {
DeleteURLMapping(ctx context.Context, instance string) error
DeleteURLMapping(ctx context.Context, instance string) (rowsAffected int64, err error)
}

type URLMapping struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/k8s_watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ func (c *Watcher) Start() error {
})

c.watch(callbackerService, c.tickerCallbacker, func(podName string) error {
_, err := c.callbackerClient.DeleteURLMapping(context.Background(), &callbacker_api.DeleteURLMappingRequest{
deleteMappingResponse, err := c.callbackerClient.DeleteURLMapping(context.Background(), &callbacker_api.DeleteURLMappingRequest{
Instance: podName,
})
if err == nil {
c.logger.Info("mapping removed", slog.String("pod-name", podName))
c.logger.Info("mapping removed", slog.String("pod-name", podName), slog.Int64("rows", deleteMappingResponse.Rows))
}
return err
})
Expand Down
5 changes: 2 additions & 3 deletions internal/k8s_watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/bitcoin-sv/arc/internal/callbacker/callbacker_api"
cbcMocks "github.com/bitcoin-sv/arc/internal/callbacker/mocks"
Expand Down Expand Up @@ -101,11 +100,11 @@ func TestStartMetamorphWatcher(t *testing.T) {
},
}
callbackerClient := &cbcMocks.CallbackerAPIClientMock{
DeleteURLMappingFunc: func(_ context.Context, _ *callbacker_api.DeleteURLMappingRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) {
DeleteURLMappingFunc: func(_ context.Context, _ *callbacker_api.DeleteURLMappingRequest, _ ...grpc.CallOption) (*callbacker_api.DeleteURLMappingResponse, error) {
if setUnlockedErrTest != nil {
return nil, setUnlockedErrTest
}
return nil, nil
return &callbacker_api.DeleteURLMappingResponse{Rows: 20}, nil
},
}

Expand Down
Loading

0 comments on commit 17a34f5

Please sign in to comment.