# domain/server - gRPC Service Implementations

**82 Go files** | Core business logic for cluster management APIs

## OVERVIEW

gRPC service layer implementing SharedPool, TiDBGroup, TiDBCluster, and backup/restore operations. Each `*_service.go` maps to proto service methods.

## STRUCTURE

```
server/
├── *_service.go          # gRPC method implementations
├── *_test.go             # Table-driven tests with mocks
├── br/                   # Backup/Restore specific services
├── mocks/                # Generated testify mocks
├── gen_component.go      # Component group generation (largest non-test file)
└── common.go             # Shared helpers
```

## WHERE TO LOOK

| Task | File | Notes |
|------|------|-------|
| Add SharedPool endpoint | `sharedpool_service.go` | Follows existing method patterns |
| Add TiDBGroup endpoint | `tidbgroup_service.go` | Similar structure |
| Modify component generation | `gen_component.go` | Complex: handles all component types |
| Add B/R endpoint | `br/*_service.go` | Backup/restore specific |

## CONVENTIONS

### Service Method Pattern
```go
func (s *SharedPoolService) ScaleSharedPool(ctx context.Context, req *apiv1.ScaleSharedPoolRequest) (*apiv1.ScaleSharedPoolResponse, error) {
    // 1. Validate request
    // 2. Load entity from repo
    // 3. Business logic
    // 4. Save to repo
    // 5. Return response
}
```

### Error Responses
```go
return nil, status.Errorf(codes.NotFound, "shared pool not found: %v", err)
return nil, status.Errorf(codes.FailedPrecondition, "cannot scale while HPA enabled")
return nil, status.Errorf(codes.InvalidArgument, "invalid node count: %d", count)
```

## ANTI-PATTERNS

- **NEVER** call workflow actions directly from service methods
- **NEVER** access dataplane-manager directly; use `pkg/dataplane` client
- **NEVER** modify entity without going through repo.Save()

## TESTING

All tests follow table-driven pattern with `setupMocks` and `validate` functions:
```go
tests := []struct {
    name          string
    setupMocks    func(*repomnocks.SharedPoolRepo, ...)
    expectedCode  codes.Code
    validate      func(*testing.T, *Response, error)
}{...}
```

Mock imports: `repomnocks "github.com/tidbcloud/cluster-service-ng/pkg/storage/repo/mocks"`
