kubernetes源码release-1.15——ThreadSafeStore

staging/src/k8s.io/client-go/tools/cache/thread_safe_store.go:37
ThreadSafeStore是一个线程安全的内存存储,
// threadSafeMap implements ThreadSafeStore
type threadSafeMap struct {
	lock  sync.RWMutex    //读写锁
	items map[string]interface{} //存储的数据,对象的key -> 对象本身

	// indexers maps a name to an IndexFunc
    // IndexFunc 特征提取函数,输入是一个对象,输出是该对象的 特征字符串列表
    // type IndexFunc func(obj interface{}) ([]string, error)
	indexers Indexers    //IndexFuncName -> IndexFunc 该threadSafeMap实例中可用的特征提取函数
	
    // indices maps a name to an Index
    //一个两级的Map
    //IndexFuncName -> 特征字符串 -> 对象的key(多个)
	indices Indices 
}

相关函数:

// deleteFromIndices removes the object from each of the managed indexes
// it is intended to be called from a function that already has a lock on the cache
// 清除给定对象的特征映射信息
func (c *threadSafeMap) deleteFromIndices(obj interface{}, key string) {
	for name, indexFunc := range c.indexers {
        //该对象的特征字符串列表,都需要清理
		indexValues, err := indexFunc(obj)
		if err != nil {
			panic(fmt.Errorf("unable to calculate an index entry for key %q on index %q: %v", key, name, err))
		}

        //特征映射,index: 特征字符串 -> 对象的Key
		index := c.indices[name]
		if index == nil {
			continue
		}
		for _, indexValue := range indexValues {
			set := index[indexValue]
			if set != nil {
				set.Delete(key)
			}
		}
	}
}

 

//更新特征映射信息
func (c *threadSafeMap) updateIndices(oldObj interface{}, newObj interface{}, key string)


//返回指定特征函数 在所有对象上的 特征列表
func (c *threadSafeMap) ListIndexFuncValues(indexName string) []string


//返回对象的key,
//indexName: 特征提取函数名
//indexKey:特征字符串
func (c *threadSafeMap) IndexKeys(indexName, indexKey string) ([]string, error) {
	c.lock.RLock()
	defer c.lock.RUnlock()

	indexFunc := c.indexers[indexName]
	if indexFunc == nil {
		return nil, fmt.Errorf("Index with name %s does not exist", indexName)
	}

	index := c.indices[indexName]

	set := index[indexKey]
	return set.List(), nil
}

 

你可能感兴趣的:(kubernetes)