| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | // Copyright 2019 The Prometheus Authors
 | 
					
						
							|  |  |  | // 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 remote | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2023-11-11 21:07:09 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2019-12-13 04:47:23 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2023-11-11 21:07:09 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 04:47:23 +08:00
										 |  |  | 	common_config "github.com/prometheus/common/config" | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2020-10-22 17:00:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/config" | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/model/labels" | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestStorageLifecycle(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-11-20 04:21:45 +08:00
										 |  |  | 	dir := t.TempDir() | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-13 20:16:33 +08:00
										 |  |  | 	s := NewStorage(nil, nil, nil, dir, defaultFlushDeadline, nil) | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 	conf := &config.Config{ | 
					
						
							|  |  |  | 		GlobalConfig: config.DefaultGlobalConfig, | 
					
						
							|  |  |  | 		RemoteWriteConfigs: []*config.RemoteWriteConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-01 00:20:16 +08:00
										 |  |  | 			// We need to set URL's so that metric creation doesn't panic.
 | 
					
						
							|  |  |  | 			baseRemoteWriteConfig("http://test-storage.com"), | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		RemoteReadConfigs: []*config.RemoteReadConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-01 00:20:16 +08:00
										 |  |  | 			baseRemoteReadConfig("http://test-storage.com"), | 
					
						
							| 
									
										
										
										
											2019-12-13 04:47:23 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, s.ApplyConfig(conf)) | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// make sure remote write has a queue.
 | 
					
						
							| 
									
										
										
										
											2023-12-07 19:35:01 +08:00
										 |  |  | 	require.Len(t, s.rws.queues, 1) | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// make sure remote write has a queue.
 | 
					
						
							| 
									
										
										
										
											2023-12-07 19:35:01 +08:00
										 |  |  | 	require.Len(t, s.queryables, 1) | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 04:21:45 +08:00
										 |  |  | 	err := s.Close() | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestUpdateRemoteReadConfigs(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-11-20 04:21:45 +08:00
										 |  |  | 	dir := t.TempDir() | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-13 20:16:33 +08:00
										 |  |  | 	s := NewStorage(nil, nil, nil, dir, defaultFlushDeadline, nil) | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	conf := &config.Config{ | 
					
						
							|  |  |  | 		GlobalConfig: config.GlobalConfig{}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, s.ApplyConfig(conf)) | 
					
						
							| 
									
										
										
										
											2023-12-07 19:35:01 +08:00
										 |  |  | 	require.Empty(t, s.queryables) | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	conf.RemoteReadConfigs = []*config.RemoteReadConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-01 00:20:16 +08:00
										 |  |  | 		baseRemoteReadConfig("http://test-storage.com"), | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, s.ApplyConfig(conf)) | 
					
						
							| 
									
										
										
										
											2023-12-07 19:35:01 +08:00
										 |  |  | 	require.Len(t, s.queryables, 1) | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 04:21:45 +08:00
										 |  |  | 	err := s.Close() | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2019-05-17 17:29:49 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestFilterExternalLabels(t *testing.T) { | 
					
						
							|  |  |  | 	dir := t.TempDir() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-13 20:16:33 +08:00
										 |  |  | 	s := NewStorage(nil, nil, nil, dir, defaultFlushDeadline, nil) | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	conf := &config.Config{ | 
					
						
							|  |  |  | 		GlobalConfig: config.GlobalConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-22 00:41:20 +08:00
										 |  |  | 			ExternalLabels: labels.FromStrings("foo", "bar"), | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	require.NoError(t, s.ApplyConfig(conf)) | 
					
						
							| 
									
										
										
										
											2023-12-07 19:35:01 +08:00
										 |  |  | 	require.Empty(t, s.queryables) | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	conf.RemoteReadConfigs = []*config.RemoteReadConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-01 00:20:16 +08:00
										 |  |  | 		baseRemoteReadConfig("http://test-storage.com"), | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	require.NoError(t, s.ApplyConfig(conf)) | 
					
						
							| 
									
										
										
										
											2023-12-07 19:35:01 +08:00
										 |  |  | 	require.Len(t, s.queryables, 1) | 
					
						
							| 
									
										
										
										
											2022-03-10 06:35:14 +08:00
										 |  |  | 	require.Equal(t, 1, s.queryables[0].(*sampleAndChunkQueryableClient).externalLabels.Len()) | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err := s.Close() | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestIgnoreExternalLabels(t *testing.T) { | 
					
						
							|  |  |  | 	dir := t.TempDir() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-13 20:16:33 +08:00
										 |  |  | 	s := NewStorage(nil, nil, nil, dir, defaultFlushDeadline, nil) | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	conf := &config.Config{ | 
					
						
							|  |  |  | 		GlobalConfig: config.GlobalConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-22 00:41:20 +08:00
										 |  |  | 			ExternalLabels: labels.FromStrings("foo", "bar"), | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	require.NoError(t, s.ApplyConfig(conf)) | 
					
						
							| 
									
										
										
										
											2023-12-07 19:35:01 +08:00
										 |  |  | 	require.Empty(t, s.queryables) | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	conf.RemoteReadConfigs = []*config.RemoteReadConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-01 00:20:16 +08:00
										 |  |  | 		baseRemoteReadConfig("http://test-storage.com"), | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	conf.RemoteReadConfigs[0].FilterExternalLabels = false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	require.NoError(t, s.ApplyConfig(conf)) | 
					
						
							| 
									
										
										
										
											2023-12-07 19:35:01 +08:00
										 |  |  | 	require.Len(t, s.queryables, 1) | 
					
						
							| 
									
										
										
										
											2022-03-10 06:35:14 +08:00
										 |  |  | 	require.Equal(t, 0, s.queryables[0].(*sampleAndChunkQueryableClient).externalLabels.Len()) | 
					
						
							| 
									
										
										
										
											2022-02-17 05:12:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err := s.Close() | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-01 00:20:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // baseRemoteWriteConfig copy values from global Default Write config
 | 
					
						
							| 
									
										
										
										
											2023-10-04 04:09:25 +08:00
										 |  |  | // to avoid change global state and cross impact test execution.
 | 
					
						
							| 
									
										
										
										
											2022-07-01 00:20:16 +08:00
										 |  |  | func baseRemoteWriteConfig(host string) *config.RemoteWriteConfig { | 
					
						
							|  |  |  | 	cfg := config.DefaultRemoteWriteConfig | 
					
						
							|  |  |  | 	cfg.URL = &common_config.URL{ | 
					
						
							|  |  |  | 		URL: &url.URL{ | 
					
						
							|  |  |  | 			Host: host, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return &cfg | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // baseRemoteReadConfig copy values from global Default Read config
 | 
					
						
							| 
									
										
										
										
											2023-10-04 04:09:25 +08:00
										 |  |  | // to avoid change global state and cross impact test execution.
 | 
					
						
							| 
									
										
										
										
											2022-07-01 00:20:16 +08:00
										 |  |  | func baseRemoteReadConfig(host string) *config.RemoteReadConfig { | 
					
						
							|  |  |  | 	cfg := config.DefaultRemoteReadConfig | 
					
						
							|  |  |  | 	cfg.URL = &common_config.URL{ | 
					
						
							|  |  |  | 		URL: &url.URL{ | 
					
						
							|  |  |  | 			Host: host, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return &cfg | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-11-11 21:07:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TestWriteStorageApplyConfigsDuringCommit helps detecting races when
 | 
					
						
							|  |  |  | // ApplyConfig runs concurrently with Notify
 | 
					
						
							|  |  |  | // See https://github.com/prometheus/prometheus/issues/12747
 | 
					
						
							|  |  |  | func TestWriteStorageApplyConfigsDuringCommit(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2025-02-13 20:16:33 +08:00
										 |  |  | 	s := NewStorage(nil, nil, nil, t.TempDir(), defaultFlushDeadline, nil) | 
					
						
							| 
									
										
										
										
											2023-11-11 21:07:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var wg sync.WaitGroup | 
					
						
							|  |  |  | 	wg.Add(2000) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	start := make(chan struct{}) | 
					
						
							|  |  |  | 	for i := 0; i < 1000; i++ { | 
					
						
							|  |  |  | 		go func(i int) { | 
					
						
							|  |  |  | 			<-start | 
					
						
							|  |  |  | 			conf := &config.Config{ | 
					
						
							|  |  |  | 				GlobalConfig: config.DefaultGlobalConfig, | 
					
						
							|  |  |  | 				RemoteWriteConfigs: []*config.RemoteWriteConfig{ | 
					
						
							|  |  |  | 					baseRemoteWriteConfig(fmt.Sprintf("http://test-%d.com", i)), | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			require.NoError(t, s.ApplyConfig(conf)) | 
					
						
							|  |  |  | 			wg.Done() | 
					
						
							|  |  |  | 		}(i) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i := 0; i < 1000; i++ { | 
					
						
							|  |  |  | 		go func() { | 
					
						
							|  |  |  | 			<-start | 
					
						
							|  |  |  | 			s.Notify() | 
					
						
							|  |  |  | 			wg.Done() | 
					
						
							|  |  |  | 		}() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	close(start) | 
					
						
							|  |  |  | 	wg.Wait() | 
					
						
							|  |  |  | } |