dataStore.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2014, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "database/sql"
  22. "encoding/json"
  23. "errors"
  24. sqlite3 "github.com/mattn/go-sqlite3"
  25. "log"
  26. "sync"
  27. "time"
  28. )
  29. type dataStore struct {
  30. init sync.Once
  31. db *sql.DB
  32. }
  33. var singleton dataStore
  34. // initDataStore initializes the singleton instance of dataStore. This
  35. // function uses a sync.Once and is safe for use by concurrent goroutines.
  36. // The underlying sql.DB connection pool is also safe.
  37. func initDataStore() {
  38. singleton.init.Do(func() {
  39. const schema = `
  40. create table if not exists serverEntry
  41. (id text not null primary key,
  42. data blob not null,
  43. rank integer not null unique);
  44. `
  45. db, err := sql.Open("sqlite3", DATA_STORE_FILENAME)
  46. if err != nil {
  47. log.Fatal("initDataStore failed to open database: %s", err)
  48. }
  49. _, err = db.Exec(schema)
  50. if err != nil {
  51. log.Fatal("initDataStore failed to initialize schema: %s", err)
  52. }
  53. singleton.db = db
  54. })
  55. }
  56. // transactionWithRetry will retry a write transaction if sqlite3
  57. // reports ErrBusy or ErrBusySnapshot -- i.e., if the XXXXX
  58. func transactionWithRetry(updater func(*sql.Tx) error) error {
  59. initDataStore()
  60. for i := 0; i < 10; i++ {
  61. transaction, err := singleton.db.Begin()
  62. if err != nil {
  63. return ContextError(err)
  64. }
  65. err = updater(transaction)
  66. if err != nil {
  67. transaction.Rollback()
  68. if sqlError, ok := err.(sqlite3.Error); ok &&
  69. (sqlError.Code == sqlite3.ErrBusy ||
  70. sqlError.ExtendedCode == sqlite3.ErrBusySnapshot) {
  71. time.Sleep(100)
  72. continue
  73. }
  74. return ContextError(err)
  75. }
  76. err = transaction.Commit()
  77. if err != nil {
  78. return ContextError(err)
  79. }
  80. return nil
  81. }
  82. return ContextError(errors.New("retries exhausted"))
  83. }
  84. // serverEntryExists returns true if a serverEntry with the
  85. // given ipAddress id already exists.
  86. func serverEntryExists(transaction *sql.Tx, ipAddress string) bool {
  87. query := "select count(*) from serverEntry where id = ?;"
  88. var count int
  89. err := singleton.db.QueryRow(query, ipAddress).Scan(&count)
  90. return err == nil && count > 0
  91. }
  92. // StoreServerEntry adds the server entry to the data store. A newly
  93. // stored (or re-stored) server entry is assigned the next-to-top rank
  94. // for cycle order (the previous top ranked entry is promoted). The
  95. // purpose of this is to keep the last selected server as the top
  96. // ranked server.
  97. // When replaceIfExists is true, an existing server entry record is
  98. // overwritten; otherwise, the existing record is unchanged.
  99. func StoreServerEntry(serverEntry *ServerEntry, replaceIfExists bool) error {
  100. return transactionWithRetry(func(transaction *sql.Tx) error {
  101. serverEntryExists := serverEntryExists(transaction, serverEntry.IpAddress)
  102. if serverEntryExists && !replaceIfExists {
  103. return nil
  104. }
  105. // TODO: also skip updates if replaceIfExists but 'data' has not changed
  106. _, err := transaction.Exec(`
  107. update serverEntry set rank = rank + 1
  108. where id = (select id from serverEntry order by rank desc limit 1);
  109. `)
  110. if err != nil {
  111. return ContextError(err)
  112. }
  113. data, err := json.Marshal(serverEntry)
  114. if err != nil {
  115. return ContextError(err)
  116. }
  117. _, err = transaction.Exec(`
  118. insert or replace into serverEntry (id, data, rank)
  119. values (?, ?, (select coalesce(max(rank)-1, 0) from serverEntry));
  120. `, serverEntry.IpAddress, data)
  121. // TODO: log after commit
  122. if !serverEntryExists {
  123. log.Printf("stored server %s", serverEntry.IpAddress)
  124. }
  125. return nil
  126. })
  127. }
  128. // PromoteServerEntry assigns the top cycle rank to the specified
  129. // server entry. This server entry will be the first candidate in
  130. // a subsequent tunnel establishment.
  131. func PromoteServerEntry(ipAddress string) error {
  132. return transactionWithRetry(func(transaction *sql.Tx) error {
  133. _, err := transaction.Exec(`
  134. update serverEntry
  135. set rank = (select MAX(rank)+1 from serverEntry)
  136. where id = ?;
  137. `, ipAddress)
  138. if err != nil {
  139. return ContextError(err)
  140. }
  141. return nil
  142. })
  143. }
  144. // ServerEntryCycler is used to continuously iterate over
  145. // stored server entries in rank order.
  146. type ServerEntryCycler struct {
  147. transaction *sql.Tx
  148. cursor *sql.Rows
  149. isReset bool
  150. }
  151. // NewServerEntryCycler creates a new ServerEntryCycler
  152. func NewServerEntryCycler() (cycler *ServerEntryCycler, err error) {
  153. initDataStore()
  154. cycler = new(ServerEntryCycler)
  155. err = cycler.Reset()
  156. if err != nil {
  157. return nil, err
  158. }
  159. return cycler, nil
  160. }
  161. // Reset a ServerEntryCycler to the start of its cycle. The next
  162. // call to Next will return the first server entry.
  163. func (cycler *ServerEntryCycler) Reset() error {
  164. cycler.Close()
  165. transaction, err := singleton.db.Begin()
  166. if err != nil {
  167. return ContextError(err)
  168. }
  169. cursor, err := transaction.Query("select * from serverEntry order by rank desc;")
  170. if err != nil {
  171. transaction.Rollback()
  172. return ContextError(err)
  173. }
  174. cycler.isReset = true
  175. cycler.transaction = transaction
  176. cycler.cursor = cursor
  177. return nil
  178. }
  179. // Close cleans up resources associated with a ServerEntryCycler.
  180. func (cycler *ServerEntryCycler) Close() {
  181. if cycler.cursor != nil {
  182. cycler.cursor.Close()
  183. }
  184. cycler.cursor = nil
  185. if cycler.transaction != nil {
  186. cycler.transaction.Rollback()
  187. }
  188. cycler.transaction = nil
  189. }
  190. // Next returns the next server entry, by rank, for a ServerEntryCycler. When
  191. // the ServerEntryCycler has worked through all known server entries, Next will
  192. // call Reset and start over and return the first server entry again.
  193. func (cycler *ServerEntryCycler) Next() (serverEntry *ServerEntry, err error) {
  194. defer func() {
  195. if err != nil {
  196. cycler.Close()
  197. }
  198. }()
  199. for !cycler.cursor.Next() {
  200. err = cycler.cursor.Err()
  201. if err != nil {
  202. return nil, ContextError(err)
  203. }
  204. if cycler.isReset {
  205. return nil, ContextError(errors.New("no server entries"))
  206. }
  207. err = cycler.Reset()
  208. if err != nil {
  209. return nil, ContextError(err)
  210. }
  211. }
  212. cycler.isReset = false
  213. var id string
  214. var data []byte
  215. var rank int64
  216. err = cycler.cursor.Scan(&id, &data, &rank)
  217. if err != nil {
  218. return nil, ContextError(err)
  219. }
  220. serverEntry = new(ServerEntry)
  221. err = json.Unmarshal(data, serverEntry)
  222. if err != nil {
  223. return nil, ContextError(err)
  224. }
  225. return serverEntry, nil
  226. }
  227. // HasServerEntries returns true if the data store contains at
  228. // least one server entry.
  229. func HasServerEntries() bool {
  230. initDataStore()
  231. var count int
  232. err := singleton.db.QueryRow("select count(*) from serverEntry;").Scan(&count)
  233. if err == nil {
  234. log.Printf("stored servers: %d", count)
  235. }
  236. return err == nil && count > 0
  237. }