Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor bulk API to minimize allocations and increase speed. #319

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 78 additions & 17 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@ Third party libraries used by the Elastic Beats project:
================================================================================


--------------------------------------------------------------------------------
Dependency : github.com/Pallinder/go-randomdata
Version: v1.2.0
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/!pallinder/[email protected]/LICENSE:

The MIT License (MIT)

Copyright (c) 2013 David Pallinder

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/aleksmaus/generate
Version: v0.0.0-20210326194607-c630e07a2742
Expand Down Expand Up @@ -2119,6 +2149,23 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/mailru/easyjson
Version: v0.7.7
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/mailru/[email protected]/LICENSE:

Copyright (c) 2016 Mail.Ru Group

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/miolini/datacounter
Version: v1.0.2
Expand Down Expand Up @@ -25696,6 +25743,37 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/josharian/intern
Version: v1.0.0
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/josharian/[email protected]/license.md:

MIT License

Copyright (c) 2019 Josh Bleecher Snyder

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/jpillora/backoff
Version: v1.0.0
Expand Down Expand Up @@ -26617,23 +26695,6 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/mailru/easyjson
Version: v0.7.1
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/mailru/[email protected]/LICENSE:

Copyright (c) 2016 Mail.Ru Group

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/markbates/pkger
Version: v0.17.0
Expand Down
124 changes: 0 additions & 124 deletions cmd/fleet/bulkCheckin.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/fleet/handleArtifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (rt Router) handleArtifacts(w http.ResponseWriter, r *http.Request, ps http
if err != nil {
code, lvl := cntArtifacts.IncError(err)

zlog.WithLevel(lvl).
log.WithLevel(lvl).
Err(err).
Int("code", code).
Int64("nWritten", nWritten).
Expand Down
62 changes: 41 additions & 21 deletions cmd/fleet/handleCheckin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/elastic/fleet-server/v7/internal/pkg/action"
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/cache"
"github.com/elastic/fleet-server/v7/internal/pkg/checkin"
"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/elastic/fleet-server/v7/internal/pkg/dl"
"github.com/elastic/fleet-server/v7/internal/pkg/limit"
Expand Down Expand Up @@ -71,7 +72,7 @@ type CheckinT struct {
verCon version.Constraints
cfg *config.Server
cache cache.Cache
bc *BulkCheckin
bc *checkin.Bulk
pm policy.Monitor
gcp monitor.GlobalCheckpointProvider
ad *action.Dispatcher
Expand All @@ -84,7 +85,7 @@ func NewCheckinT(
verCon version.Constraints,
cfg *config.Server,
c cache.Cache,
bc *BulkCheckin,
bc *checkin.Bulk,
pm policy.Monitor,
gcp monitor.GlobalCheckpointProvider,
ad *action.Dispatcher,
Expand Down Expand Up @@ -151,7 +152,7 @@ func (ct *CheckinT) _handleCheckin(w http.ResponseWriter, r *http.Request, id st
cntCheckin.bodyIn.Add(readCounter.Count())

// Compare local_metadata content and update if different
fields, err := parseMeta(agent, &req)
rawMeta, err := parseMeta(agent, &req)
if err != nil {
return err
}
Expand Down Expand Up @@ -183,7 +184,7 @@ func (ct *CheckinT) _handleCheckin(w http.ResponseWriter, r *http.Request, id st
defer longPoll.Stop()

// Intial update on checkin, and any user fields that might have changed
ct.bc.CheckIn(agent.Id, fields, seqno)
ct.bc.CheckIn(agent.Id, rawMeta, seqno)

// Initial fetch for pending actions
var (
Expand Down Expand Up @@ -220,7 +221,7 @@ func (ct *CheckinT) _handleCheckin(w http.ResponseWriter, r *http.Request, id st
log.Trace().Msg("fire long poll")
break LOOP
case <-tick.C:
ct.bc.CheckIn(agent.Id, nil, seqno)
ct.bc.CheckIn(agent.Id, nil, nil)
scunningham marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -511,31 +512,50 @@ func findAgentByApiKeyId(ctx context.Context, bulker bulk.Bulk, id string) (*mod

// parseMeta compares the agent and the request local_metadata content
// and returns fields to update the agent record or nil
func parseMeta(agent *model.Agent, req *CheckinRequest) (fields Fields, err error) {
// Quick comparison first
func parseMeta(agent *model.Agent, req *CheckinRequest) ([]byte, error) {

// Quick comparison first; compare the JSON payloads.
// If the data is not consistently normalized, this short-circuit will not work.
if bytes.Equal(req.LocalMeta, agent.LocalMetadata) {
log.Trace().Msg("quick comparing local metadata is equal")
return nil, nil
}

// Compare local_metadata content and update if different
var reqLocalMeta Fields
var agentLocalMeta Fields
err = json.Unmarshal(req.LocalMeta, &reqLocalMeta)
if err != nil {
// Deserialize the request metadata
var reqLocalMeta interface{}
if err := json.Unmarshal(req.LocalMeta, &reqLocalMeta); err != nil {
return nil, err
}
err = json.Unmarshal(agent.LocalMetadata, &agentLocalMeta)
if err != nil {

// If empty, don't step on existing data
if reqLocalMeta == nil {
return nil, nil
}

// Deserialize the agent's metadata copy
var agentLocalMeta interface{}
if err := json.Unmarshal(agent.LocalMetadata, &agentLocalMeta); err != nil {
return nil, err
}

if reqLocalMeta != nil && !reflect.DeepEqual(reqLocalMeta, agentLocalMeta) {
log.Trace().RawJSON("oldLocalMeta", agent.LocalMetadata).RawJSON("newLocalMeta", req.LocalMeta).Msg("local metadata not equal")
log.Info().RawJSON("req.LocalMeta", req.LocalMeta).Msg("applying new local metadata")
fields = map[string]interface{}{
FieldLocalMetadata: req.LocalMeta,
}
var outMeta []byte

// Compare the deserialized meta structures and return the bytes to update if different
if !reflect.DeepEqual(reqLocalMeta, agentLocalMeta) {

log.Trace().
Str("agentId", agent.Id).
RawJSON("oldLocalMeta", agent.LocalMetadata).
RawJSON("newLocalMeta", req.LocalMeta).
Msg("local metadata not equal")

log.Info().
Str("agentId", agent.Id).
RawJSON("req.LocalMeta", req.LocalMeta).
Msg("applying new local metadata")

outMeta = req.LocalMeta
}
return fields, nil

return outMeta, nil
}
3 changes: 2 additions & 1 deletion cmd/fleet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/elastic/fleet-server/v7/internal/pkg/action"
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/cache"
"github.com/elastic/fleet-server/v7/internal/pkg/checkin"
"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/elastic/fleet-server/v7/internal/pkg/coordinator"
"github.com/elastic/fleet-server/v7/internal/pkg/dl"
Expand Down Expand Up @@ -588,7 +589,7 @@ func (f *FleetServer) runServer(ctx context.Context, cfg *config.Config) (err er
return err
}

bc := NewBulkCheckin(bulker)
bc := checkin.NewBulk(bulker)
g.Go(loggedRunFunc(ctx, "Bulk checkin", bc.Run))

ct := NewCheckinT(f.verCon, &f.cfg.Inputs[0].Server, f.cache, bc, pm, am, ad, tr, bulker)
Expand Down
Loading