collection_replica.go 6.8 KB
Newer Older
X
XuanYang-cn 已提交
1 2 3
package datanode

import (
4
	"log"
X
XuanYang-cn 已提交
5 6 7
	"sync"

	"github.com/zilliztech/milvus-distributed/internal/errors"
8
	"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
X
XuanYang-cn 已提交
9 10 11 12 13 14 15 16 17 18
)

type collectionReplica interface {

	// collection
	getCollectionNum() int
	addCollection(collectionID UniqueID, schemaBlob string) error
	removeCollection(collectionID UniqueID) error
	getCollectionByID(collectionID UniqueID) (*Collection, error)
	getCollectionByName(collectionName string) (*Collection, error)
X
XuanYang-cn 已提交
19
	getCollectionIDByName(collectionName string) (UniqueID, error)
X
XuanYang-cn 已提交
20 21
	hasCollection(collectionID UniqueID) bool

22
	// segment
X
XuanYang-cn 已提交
23 24
	addSegment(segmentID UniqueID, collID UniqueID, partitionID UniqueID,
		createTime Timestamp, positions []*internalpb2.MsgPosition) error
25 26
	removeSegment(segmentID UniqueID) error
	hasSegment(segmentID UniqueID) bool
X
XuanYang-cn 已提交
27 28
	updateStatistics(segmentID UniqueID, numRows int64, endTime Timestamp,
		positions []*internalpb2.MsgPosition) error
29
	getSegmentStatisticsUpdates(segmentID UniqueID) (*internalpb2.SegmentStatisticsUpdates, error)
X
XuanYang-cn 已提交
30
	getSegmentByID(segmentID UniqueID) (*Segment, error)
X
XuanYang-cn 已提交
31 32
}

33 34
type (
	Segment struct {
X
XuanYang-cn 已提交
35 36 37 38 39 40 41 42 43 44
		segmentID      UniqueID
		collectionID   UniqueID
		partitionID    UniqueID
		numRows        int64
		memorySize     int64
		isNew          bool
		createTime     Timestamp
		endTime        Timestamp
		startPositions []*internalpb2.MsgPosition
		endPositions   []*internalpb2.MsgPosition
45 46 47 48 49 50 51 52 53
	}

	collectionReplicaImpl struct {
		mu          sync.RWMutex
		collections []*Collection
		segments    []*Segment
	}
)

X
XuanYang-cn 已提交
54
//----------------------------------------------------------------------------------------------------- collection
X
XuanYang-cn 已提交
55
func (colReplica *collectionReplicaImpl) getSegmentByID(segmentID UniqueID) (*Segment, error) {
56 57
	colReplica.mu.RLock()
	defer colReplica.mu.RUnlock()
X
XuanYang-cn 已提交
58 59 60 61 62 63 64 65 66

	for _, segment := range colReplica.segments {
		if segment.segmentID == segmentID {
			return segment, nil
		}
	}
	return nil, errors.Errorf("cannot find segment, id = %v", segmentID)
}

X
XuanYang-cn 已提交
67 68 69
func (colReplica *collectionReplicaImpl) addSegment(segmentID UniqueID, collID UniqueID,
	partitionID UniqueID, createTime Timestamp, positions []*internalpb2.MsgPosition) error {

X
XuanYang-cn 已提交
70 71
	colReplica.mu.Lock()
	defer colReplica.mu.Unlock()
72
	log.Println("Add Segment", segmentID)
X
XuanYang-cn 已提交
73

74
	seg := &Segment{
X
XuanYang-cn 已提交
75 76 77 78 79 80 81
		segmentID:      segmentID,
		collectionID:   collID,
		partitionID:    partitionID,
		isNew:          true,
		createTime:     createTime,
		startPositions: positions,
		endPositions:   make([]*internalpb2.MsgPosition, 0),
82 83 84 85 86 87
	}
	colReplica.segments = append(colReplica.segments, seg)
	return nil
}

func (colReplica *collectionReplicaImpl) removeSegment(segmentID UniqueID) error {
X
XuanYang-cn 已提交
88 89
	colReplica.mu.Lock()
	defer colReplica.mu.Unlock()
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

	for index, ele := range colReplica.segments {
		if ele.segmentID == segmentID {
			log.Println("Removing segment:", segmentID)
			numOfSegs := len(colReplica.segments)
			colReplica.segments[index] = colReplica.segments[numOfSegs-1]
			colReplica.segments = colReplica.segments[:numOfSegs-1]
			return nil
		}
	}
	return errors.Errorf("Error, there's no segment %v", segmentID)
}

func (colReplica *collectionReplicaImpl) hasSegment(segmentID UniqueID) bool {
	colReplica.mu.RLock()
	defer colReplica.mu.RUnlock()

	for _, ele := range colReplica.segments {
		if ele.segmentID == segmentID {
			return true
		}
	}
	return false
}

X
XuanYang-cn 已提交
115
func (colReplica *collectionReplicaImpl) updateStatistics(segmentID UniqueID, numRows int64, endTime Timestamp, positions []*internalpb2.MsgPosition) error {
X
XuanYang-cn 已提交
116 117
	colReplica.mu.Lock()
	defer colReplica.mu.Unlock()
118 119 120 121 122 123

	for _, ele := range colReplica.segments {
		if ele.segmentID == segmentID {
			log.Printf("updating segment(%v) row nums: (%v)", segmentID, numRows)
			ele.memorySize = 0
			ele.numRows += numRows
X
XuanYang-cn 已提交
124 125
			ele.endTime = endTime
			ele.endPositions = positions
126 127 128 129 130 131 132
			return nil
		}
	}
	return errors.Errorf("Error, there's no segment %v", segmentID)
}

func (colReplica *collectionReplicaImpl) getSegmentStatisticsUpdates(segmentID UniqueID) (*internalpb2.SegmentStatisticsUpdates, error) {
X
XuanYang-cn 已提交
133 134
	colReplica.mu.Lock()
	defer colReplica.mu.Unlock()
135 136 137 138

	for _, ele := range colReplica.segments {
		if ele.segmentID == segmentID {
			updates := &internalpb2.SegmentStatisticsUpdates{
X
XuanYang-cn 已提交
139 140 141 142 143 144 145 146 147 148 149 150
				SegmentID:      segmentID,
				MemorySize:     ele.memorySize,
				NumRows:        ele.numRows,
				IsNewSegment:   ele.isNew,
				CreateTime:     ele.createTime,
				EndTime:        ele.endTime,
				StartPositions: ele.startPositions,
				EndPositions:   ele.endPositions,
			}

			if ele.isNew {
				ele.isNew = false
151 152 153 154 155
			}
			return updates, nil
		}
	}
	return nil, errors.Errorf("Error, there's no segment %v", segmentID)
X
XuanYang-cn 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
}

func (colReplica *collectionReplicaImpl) getCollectionNum() int {
	colReplica.mu.RLock()
	defer colReplica.mu.RUnlock()

	return len(colReplica.collections)
}

func (colReplica *collectionReplicaImpl) addCollection(collectionID UniqueID, schemaBlob string) error {
	colReplica.mu.Lock()
	defer colReplica.mu.Unlock()

	var newCollection = newCollection(collectionID, schemaBlob)
	colReplica.collections = append(colReplica.collections, newCollection)
171
	log.Println("Create collection: ", newCollection.Name())
X
XuanYang-cn 已提交
172 173 174 175

	return nil
}

X
XuanYang-cn 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188
func (colReplica *collectionReplicaImpl) getCollectionIDByName(collName string) (UniqueID, error) {
	colReplica.mu.RLock()
	defer colReplica.mu.RUnlock()

	for _, collection := range colReplica.collections {
		if collection.Name() == collName {
			return collection.ID(), nil
		}
	}
	return 0, errors.Errorf("There is no collection name=%v", collName)

}

X
XuanYang-cn 已提交
189
func (colReplica *collectionReplicaImpl) removeCollection(collectionID UniqueID) error {
190
	// GOOSE TODO: optimize
X
XuanYang-cn 已提交
191 192 193 194 195 196 197 198
	colReplica.mu.Lock()
	defer colReplica.mu.Unlock()

	tmpCollections := make([]*Collection, 0)
	for _, col := range colReplica.collections {
		if col.ID() != collectionID {
			tmpCollections = append(tmpCollections, col)
		} else {
199
			log.Println("Drop collection : ", col.Name())
X
XuanYang-cn 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		}
	}
	colReplica.collections = tmpCollections
	return nil
}

func (colReplica *collectionReplicaImpl) getCollectionByID(collectionID UniqueID) (*Collection, error) {
	colReplica.mu.RLock()
	defer colReplica.mu.RUnlock()

	for _, collection := range colReplica.collections {
		if collection.ID() == collectionID {
			return collection, nil
		}
	}
215
	return nil, errors.Errorf("cannot find collection, id = %v", collectionID)
X
XuanYang-cn 已提交
216 217 218 219 220 221 222 223 224 225 226 227
}

func (colReplica *collectionReplicaImpl) getCollectionByName(collectionName string) (*Collection, error) {
	colReplica.mu.RLock()
	defer colReplica.mu.RUnlock()

	for _, collection := range colReplica.collections {
		if collection.Name() == collectionName {
			return collection, nil
		}
	}

228
	return nil, errors.Errorf("Cannot found collection: %v", collectionName)
X
XuanYang-cn 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241
}

func (colReplica *collectionReplicaImpl) hasCollection(collectionID UniqueID) bool {
	colReplica.mu.RLock()
	defer colReplica.mu.RUnlock()

	for _, col := range colReplica.collections {
		if col.ID() == collectionID {
			return true
		}
	}
	return false
}