Skip to content

Commit f9ea5e4

Browse files
simonmarfacebook-github-bot
authored andcommitted
Add an "auto" repo mapping (#566)
Summary: When using AutoRepoMapping, repo names are assumed to correspond 1:1 with Glean DB names. This is convenient for a pre-built Glass binary because we don't know the repos and DBs at build-time. Pull Request resolved: #566 Reviewed By: donsbot Differential Revision: D78554418 Pulled By: pepeiborra fbshipit-source-id: 592b0d27f914f4bded12cb0325e3f90c4f786c30
1 parent 167195d commit f9ea5e4

3 files changed

Lines changed: 55 additions & 85 deletions

File tree

glean/glass/Glean/Glass/Base.hs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,32 @@ data GleanDBSelector = GleanDBSelector
8181
-- ^ Nothing means the db is default, can be used for any branch
8282
}
8383

84-
data RepoMapping = RepoMapping
85-
{ gleanIndices :: Map.Map Glass.RepoName [GleanDBSelector]
86-
-- ^ This should be in a config
84+
data RepoMapping
85+
= AutoRepoMapping
86+
-- ^ Repo name == Glean DB name. e.g. if we get a documentSymbols
87+
-- request for myrepo/lang/My/File then we query the latest
88+
-- "myrepo" Glean DB.
8789
--
88-
-- This is the set of Glean dbs that should implement codemarkup.*
89-
--
90-
-- Note: the order here determines the order of search/lookup
91-
-- if you have overlapping dbs , for file contents, the first in the order
92-
-- with a src.File fact will win.
93-
--
94-
-- If you add/remove a db, consider if it needs to be present in
95-
-- gleanRequiredIndices as well
90+
-- In this case 'RepoMapping.allGleanRepos' can be 'Nothing' to
91+
-- enable use of any Glean DB, or it can be set to a list to
92+
-- restrict Glass to using particular Glean DBs.
9693

97-
, gleanAttrIndices :: Map.Map Glass.RepoName [GleanDBAttrName]
98-
-- ^ Map of source db to attr db names & attribute key types
99-
--
100-
-- This pairs attribute Glean dbs with a key type to index the ToAttribute
101-
-- class, that in turns knowns how to query and marshal the attributes
102-
}
94+
| RepoMapping
95+
{ gleanIndices :: Map.Map Glass.RepoName [GleanDBSelector]
96+
-- ^ This should be in a config
97+
--
98+
-- This is the set of Glean dbs that should implement codemarkup.*
99+
--
100+
-- Note: the order here determines the order of search/lookup
101+
-- if you have overlapping dbs , for file contents, the first in the order
102+
-- with a src.File fact will win.
103+
--
104+
-- If you add/remove a db, consider if it needs to be present in
105+
-- gleanRequiredIndices as well
106+
107+
, gleanAttrIndices :: Map.Map Glass.RepoName [GleanDBAttrName]
108+
-- ^ Map of source db to attr db names & attribute key types
109+
--
110+
-- This pairs attribute Glean dbs with a key type to index the ToAttribute
111+
-- class, that in turns knowns how to query and marshal the attributes
112+
}

glean/glass/Glean/Glass/RepoMapping.hs

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,71 +18,25 @@ module Glean.Glass.RepoMapping
1818

1919
import Data.Set (Set)
2020
import qualified Data.Set as Set
21-
import qualified Data.Map.Strict as Map
21+
import Data.Text(Text)
2222

2323
import Glean.Glass.Base
2424
( GleanDBName(..)
2525
, RepoMapping(..)
26-
, GleanDBSelector(..)
2726
)
28-
import Glean.Glass.Types ( Language(..), RepoName(..) )
29-
import Data.Text(Text)
27+
import Glean.Glass.Types ( RepoName(..) )
3028

3129
getRepoMapping :: IO RepoMapping
32-
getRepoMapping = return RepoMapping
33-
{ gleanIndices = gleanIndices_
34-
, gleanAttrIndices = Map.empty
35-
}
30+
getRepoMapping = return AutoRepoMapping
3631

3732
fixedRepoMapping :: RepoMapping
38-
fixedRepoMapping = RepoMapping
39-
{ gleanIndices = gleanIndices_
40-
, gleanAttrIndices = Map.empty
41-
}
42-
43-
-- example: the open source react repo.
44-
gleanIndices_ :: Map.Map RepoName [GleanDBSelector]
45-
gleanIndices_ = Map.fromList
46-
-- demo
47-
[ ( RepoName "react",
48-
[ GleanDBSelector
49-
{ dbName = "react"
50-
, language = Language_JavaScript
51-
, branchName = Nothing
52-
}
53-
]
54-
)
55-
-- for running tests with locally-indexed repos:
56-
, ( RepoName "test",
57-
[ testSelector Language_JavaScript
58-
, testSelector Language_Hack
59-
, testSelector Language_Haskell
60-
, testSelector Language_Cpp
61-
, testSelector Language_PreProcessor
62-
, testSelector Language_Python
63-
, testSelector Language_Thrift
64-
, testSelector Language_Buck
65-
, testSelector Language_Go
66-
, testSelector Language_TypeScript
67-
, testSelector Language_Rust
68-
, testSelector Language_Java
69-
, testSelector Language_Swift
70-
]
71-
)
72-
]
73-
where
74-
testSelector language =
75-
GleanDBSelector
76-
{ dbName = "test"
77-
, language = language
78-
, branchName = Nothing
79-
}
33+
fixedRepoMapping = AutoRepoMapping
8034

8135
-- | All the Glean db repo names we're aware of
82-
-- We will only be able to query members of this set
83-
allGleanRepos :: Set GleanDBName
84-
allGleanRepos = Set.fromList $
85-
map dbName (concat (Map.elems gleanIndices_))
36+
-- We will only be able to query members of this set.
37+
-- 'Nothing' means all existing Glean DBs can be used.
38+
allGleanRepos :: Maybe (Set GleanDBName)
39+
allGleanRepos = Nothing
8640

8741
-- repos that are required
8842
gleanRequiredIndices :: Set.Set GleanDBName

glean/glass/Glean/Glass/Repos.hs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ data GleanDBInfo = GleanDBInfo
104104

105105
-- return a RepoName if indexed by glean
106106
toRepoName :: RepoMapping -> Text -> Maybe RepoName
107+
toRepoName AutoRepoMapping repo = Just (RepoName repo)
107108
toRepoName RepoMapping{..} repo =
108109
case Map.lookup repoName gleanIndices of
109110
Just _ -> Just repoName
@@ -113,6 +114,7 @@ toRepoName RepoMapping{..} repo =
113114

114115
-- | Additional metadata about files and methods in attribute dbs
115116
attrDBsForRepo :: RepoMapping -> RepoName -> [GleanDBAttrName]
117+
attrDBsForRepo AutoRepoMapping _ = []
116118
attrDBsForRepo RepoMapping{..} repo =
117119
Map.findWithDefault [] repo gleanAttrIndices
118120

@@ -145,6 +147,12 @@ selectGleanDBs
145147
-> Maybe RepoName
146148
-> Set Language
147149
-> Either Text (Map.Map RepoName (Set GleanDBName))
150+
selectGleanDBs AutoRepoMapping (Just repoName) _langs =
151+
Right (
152+
Map.singleton repoName (
153+
Set.singleton (GleanDBName (unRepoName repoName))))
154+
selectGleanDBs AutoRepoMapping Nothing _langs =
155+
Right Map.empty
148156
selectGleanDBs repoMapping mRepoName langs0 =
149157
case map flatten (filter matches candidates) of
150158
[] -> Left err
@@ -182,6 +190,7 @@ normalizeLanguages l = l
182190
-- | Select universe of glean repo,(db/language) pairs.
183191
-- Either just the test dbs, or all the non-test dbs.
184192
listGleanIndices :: RepoMapping -> Bool -> [(RepoName, GleanDBSelector)]
193+
listGleanIndices AutoRepoMapping _ = [] -- handled earlier
185194
listGleanIndices RepoMapping{..} testsOnly =
186195
let testRepos = [RepoName "test", RepoName "test-xlang"]
187196
flatten (repo,langs) = map (repo,) langs
@@ -199,6 +208,8 @@ fromSCSRepo
199208
-> Maybe (Text -> IO Bool)
200209
-> Maybe Language
201210
-> IO [GleanDBName]
211+
fromSCSRepo AutoRepoMapping repo _branchFilter _mLanguage =
212+
return [GleanDBName (unRepoName repo)]
202213
fromSCSRepo RepoMapping{..} repo branchFilter mLanguage
203214
| Just rs <- Map.lookup repo gleanIndices = do
204215
let filteredByLang = filterByLanguage mLanguage rs
@@ -279,7 +290,7 @@ fileLanguage (Path file)
279290
is a = a `Text.isSuffixOf` file
280291

281292
--
282-
-- Operating on the latest repo statea
293+
-- Operating on the latest repo state
283294
--
284295

285296
-- | Fetch all latest dbs we care for
@@ -295,7 +306,7 @@ getLatestRepos backend scm mlogger mretry = go mretry
295306
go :: Maybe Int -> IO GleanDBInfo
296307
go n = do
297308
latest <- Glean.getLatestRepos backend $ \name ->
298-
GleanDBName name `Set.member` Mapping.allGleanRepos
309+
maybe True (GleanDBName name `Set.member`) Mapping.allGleanRepos
299310

300311
-- Filter unavailable DBs using hasDatabase. Glean.getLatestRepos does
301312
-- this for latestRepos but not allLatestRepos, in that case it's up to
@@ -317,21 +328,16 @@ getLatestRepos backend scm mlogger mretry = go mretry
317328
, scmRevisions = scmRevisions
318329
, dbByRevision = getDbByRevision dbs scmRevisions
319330
}
320-
if required `Set.isSubsetOf` advertised
321-
then return info
322-
else do
331+
required = Set.map unGleanDBName Mapping.gleanRequiredIndices
332+
missing = required `Set.difference` advertised
333+
if
334+
| not (Set.null missing), Just m <- n, m > 1 -> do
323335
-- some required dbs are missing! this is transient? and bad
324336
-- in prod/full service mode this would be bad
325-
let missing = required `Set.difference` advertised
337+
-- if no retries allowed, give up
326338
logIt mlogger dbs missing n backend
327-
case n of
328-
Just n
329-
| n > 1 -> do {- i.e. try more than 1 time -}
330-
delay (seconds 1) >> go (Just (n-1))
331-
332-
_ -> return info -- if no retries allowed, give up
333-
334-
required = Set.map unGleanDBName Mapping.gleanRequiredIndices
339+
delay (seconds 1) >> go (Just (m-1))
340+
| otherwise -> return info
335341

336342
-- | Log an entry in glean_glass_server_error_events if a logger is available,
337343
-- and locally (e.g. to stderr). Do not log otherwise (e.g. in test mode).

0 commit comments

Comments
 (0)