-
Notifications
You must be signed in to change notification settings - Fork 5
/
delta.go
221 lines (192 loc) · 6.1 KB
/
delta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// Copyright © 2017-2019 Solus Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package jobs
import (
"fmt"
"os"
"sort"
log "github.com/sirupsen/logrus"
"github.com/getsolus/ferryd/src/ferryd/core"
"github.com/getsolus/ferryd/src/libeopkg"
)
// DeltaJobHandler is responsible for indexing repositories and should only
// ever be used in async queues. Deltas may take some time to produce and
// shouldn't be allowed to block the sequential processing queue.
type DeltaJobHandler struct {
repoID string
packageName string
indexRepo bool
nDeltas int // Track how many deltas we actually produce
}
// NewDeltaJob will return a job suitable for adding to the job processor
func NewDeltaJob(repoID, packageID string) *JobEntry {
return &JobEntry{
sequential: false,
Type: Delta,
Params: []string{repoID, packageID},
}
}
// NewDeltaIndexJob will return a new job for creating delta packages as well
// as scheduling an index operation when complete.
func NewDeltaIndexJob(repoID, packageID string) *JobEntry {
return &JobEntry{
sequential: false,
Type: DeltaIndex,
Params: []string{repoID, packageID},
}
}
// NewDeltaJobHandler will create a job handler for the input job and ensure it validates
func NewDeltaJobHandler(j *JobEntry, indexRepo bool) (*DeltaJobHandler, error) {
if len(j.Params) != 2 {
return nil, fmt.Errorf("job has invalid parameters")
}
return &DeltaJobHandler{
repoID: j.Params[0],
packageName: j.Params[1],
indexRepo: indexRepo,
nDeltas: 0,
}, nil
}
// executeInternal is the common code shared in the delta jobs, and is
// split out to save duplication.
func (j *DeltaJobHandler) executeInternal(manager *core.Manager) error {
pkgs, err := manager.GetPackages(j.repoID, j.packageName)
if err != nil {
return err
}
// Need at least 2 packages for a delta op.
if len(pkgs) < 2 {
log.WithFields(log.Fields{
"repo": j.repoID,
"package": j.packageName,
}).Debug("No delta is possible")
return nil
}
sort.Sort(libeopkg.PackageSet(pkgs))
tip := pkgs[len(pkgs)-1]
// Process all potential deltas
for i := 0; i < len(pkgs)-1; i++ {
old := pkgs[i]
fields := log.Fields{
"old": old.GetID(),
"new": tip.GetID(),
"repo": j.repoID,
}
deltaID := libeopkg.ComputeDeltaName(old, tip)
// Don't need to report that it failed, we know this from history
if manager.GetDeltaFailed(deltaID) {
continue
}
hasDelta, err := manager.HasDelta(j.repoID, j.packageName, deltaID)
if err != nil {
return err
}
// Package has this delta already? Continue.
if hasDelta {
continue
}
mapping := &core.DeltaInformation{
FromID: old.GetID(),
ToID: tip.GetID(),
FromRelease: old.GetRelease(),
ToRelease: tip.GetRelease(),
}
// Before we go off creating it - does the delta package exist already?
// If so, just re-ref it for usage within the new repo
entry, err := manager.GetPoolEntry(deltaID)
if entry != nil && err == nil {
if err := manager.RefDelta(j.repoID, deltaID); err != nil {
fields["error"] = err
log.WithFields(fields).Error("Failed to ref existing delta")
return err
}
log.WithFields(fields).Info("Reused existing delta")
continue
}
deltaPath, err := manager.CreateDelta(j.repoID, old, tip)
if err != nil {
fields["error"] = err
if err == libeopkg.ErrDeltaPointless {
// Non-fatal, ask the manager to record this delta as a no-go
log.WithFields(fields).Info("Delta not possible, marked permanently")
if err := manager.MarkDeltaFailed(deltaID, mapping); err != nil {
fields["error"] = err
log.WithFields(fields).Error("Failed to mark delta failure")
return err
}
continue
} else if err == libeopkg.ErrMismatchedDelta {
log.WithFields(fields).Error("Package delta candidates do not match")
continue
} else {
// Genuinely an issue now
log.WithFields(fields).Error("Error in delta production")
return err
}
}
j.nDeltas++
fields["path"] = deltaPath
// Produced a delta!
log.WithFields(fields).Info("Successfully producing delta package")
// Let's get it included now.
if err = j.includeDelta(manager, mapping, deltaPath); err != nil {
fields["error"] = err
log.WithFields(fields).Error("Failed to include delta package")
return err
}
}
return nil
}
// includeDelta will wrap up the basic functionality to get a delta package
// imported into a target repository.
func (j *DeltaJobHandler) includeDelta(manager *core.Manager, mapping *core.DeltaInformation, deltaPath string) error {
// Try to insert the delta
if err := manager.AddDelta(j.repoID, deltaPath, mapping); err != nil {
return err
}
// Delete the deltaPath if the add is successful
return os.Remove(deltaPath)
}
// Execute will delta the target package within the target repository.
func (j *DeltaJobHandler) Execute(_ *Processor, manager *core.Manager) error {
err := j.executeInternal(manager)
if err != nil {
return err
}
if !j.indexRepo {
return nil
}
// Ask that our repository now be reindexed because we've added deltas but
// only if we've successfully produced some delta packages
if j.nDeltas < 1 {
return nil
}
if err := manager.Index(j.repoID); err != nil {
log.WithFields(log.Fields{
"repo": j.repoID,
"error": err,
}).Error("Failed to index repository")
return err
}
return nil
}
// Describe returns a human readable description for this job
func (j *DeltaJobHandler) Describe() string {
if j.indexRepo {
return fmt.Sprintf("Delta package '%s' on '%s', then re-index", j.packageName, j.repoID)
}
return fmt.Sprintf("Delta package '%s' on '%s'", j.packageName, j.repoID)
}