Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Implement randLog in keyserver
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbelvin committed Nov 6, 2019
1 parent eed581a commit 7089b59
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
21 changes: 19 additions & 2 deletions core/keyserver/keyserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package keyserver
import (
"context"
"fmt"
"math/rand"
"runtime"
"sync"
"time"
Expand Down Expand Up @@ -77,6 +78,8 @@ type MutationLogs interface {
// needed to do so.
ReadLog(ctx context.Context, directoryID string, logID int64, low, high time.Time,
limit int32) ([]*mutator.LogMessage, error)
// ListLogs returns a list of logs, optionally filtered by the writable bit.
ListLogs(ctx context.Context, directoryID string, writable bool) ([]int64, error)
}

// BatchReader reads batch definitions.
Expand Down Expand Up @@ -610,7 +613,6 @@ func (s *Server) QueueEntryUpdate(ctx context.Context, in *pb.UpdateEntryRequest
return s.BatchQueueUserUpdate(ctx, &pb.BatchQueueUserUpdateRequest{
DirectoryId: in.DirectoryId,
Updates: []*pb.EntryUpdate{in.EntryUpdate},
LogId: in.GetLogId(),
})
}

Expand Down Expand Up @@ -657,7 +659,10 @@ func (s *Server) BatchQueueUserUpdate(ctx context.Context, in *pb.BatchQueueUser
tdone()

// Save mutation to the database.
wmLogID := in.GetLogId()
wmLogID, err := s.randLog(ctx, directory.DirectoryID)
if st := status.Convert(err); st.Code() != codes.OK {
return nil, status.Errorf(st.Code(), "Could not pick a log to write to: %v", err)
}
wmTime, err := s.logs.Send(ctx, directory.DirectoryID, wmLogID, in.Updates...)
if st := status.Convert(err); st.Code() != codes.OK {
glog.Errorf("mutations.Write failed: %v", err)
Expand All @@ -668,6 +673,18 @@ func (s *Server) BatchQueueUserUpdate(ctx context.Context, in *pb.BatchQueueUser
return &empty.Empty{}, nil
}

func (s *Server) randLog(ctx context.Context, directoryID string) (int64, error) {
// TODO(gbelvin): Cache these results.
writable := true
logIDs, err := s.logs.ListLogs(ctx, directoryID, writable)
if err != nil {
return 0, err
}

// Return a random log.
return logIDs[rand.Intn(len(logIDs))], nil
}

// GetDirectory returns all info tied to the specified directory.
//
// This API to get all necessary data needed to verify a particular
Expand Down
8 changes: 8 additions & 0 deletions core/keyserver/revisions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ func (m *mutations) Send(ctx context.Context, dirID string, _ int64, mutation ..
return time.Time{}, errors.New("unimplemented")
}

func (m *mutations) ListLogs(ctx context.Context, dirID string, _ bool) ([]int64, error) {
logIDs := []int64{}
for id := range *m {
logIDs = append(logIDs, id)
}
return logIDs, nil
}

func (m *mutations) ReadLog(ctx context.Context, dirID string,
logID int64, low, high time.Time, batchSize int32) ([]*mutator.LogMessage, error) {
logShard := (*m)[logID]
Expand Down

0 comments on commit 7089b59

Please sign in to comment.