Duan Wenbo avatar Duan Wenbo committed 1d6fc43

fix

Comments (0)

Files changed (1)

     end
 end
 for i, v in ipairs(log.funcs) do
-    log[v] = function(m) 
-        log.log(log.level[v], m) 
+    log[v] = function(m)
+        log.log(log.level[v], m)
     end
 end
 --]=]
 
 
 function idle_failsafe_rw()
-    local backend_ndx = 0 
+    local backend_ndx = 0
 
     for i = 1, #proxy.global.backends do
         local s = proxy.global.backends[i]
         local conns = s.pool.users[proxy.connection.client.username]
 
-        if conns.cur_idle_connections > 0 and 
-            s.state ~= proxy.BACKEND_STATE_DOWN and 
+        if conns.cur_idle_connections > 0 and
+            s.state ~= proxy.BACKEND_STATE_DOWN and
             s.type == proxy.BACKEND_TYPE_RW then
-            backend_ndx = i 
+            backend_ndx = i
             break
-        end 
-    end 
+        end
+    end
     return backend_ndx
 end
 
-function idle_ro() 
+function idle_ro()
     local max_conns = -1
-    local max_conns_ndx = 0 
+    local max_conns_ndx = 0
     local index = proxy.global.balance.query_index
     local num = #proxy.global.backends
     for j = 1, num do
         local conns = s.pool.users[proxy.connection.client.username]
 
         -- pick a slave which has some idling connections
-        if s.type == proxy.BACKEND_TYPE_RO and 
-            s.state ~= proxy.BACKEND_STATE_DOWN and 
+        if s.type == proxy.BACKEND_TYPE_RO and
+            s.state ~= proxy.BACKEND_STATE_DOWN and
             conns.cur_idle_connections > 0 then
-            if max_conns == -1 or  
+            if max_conns == -1 or
                 s.connected_clients < max_conns then
                 max_conns = s.connected_clients
-                max_conns_ndx = index 
-            end 
+                max_conns_ndx = index
+            end
         end
         index = index + 1
-    end 
+    end
 
     if max_conns_ndx > 0 then
         proxy.connection.backend_ndx = max_conns_ndx
-        proxy.global.balance.query_index = max_conns_ndx + 1 
+        proxy.global.balance.query_index = max_conns_ndx + 1
     else
         --proxy.global.balance.query_index = 2
-        proxy.global.balance.query_index = math.random(1, num) 
+        proxy.global.balance.query_index = math.random(1, num)
     end
 end
 
--- read/write splitting sends all non-transactional SELECTs to the 
+-- read/write splitting sends all non-transactional SELECTs to the
 -- slaves is_in_transaction tracks the state of the transactions
 local is_in_transaction            = false
 
         print("[error] least_idle_conns_ndx <= 0 in connect_server")
         assert(false)
     end
-    -- open a new connection 
+    -- open a new connection
     ---[=[ @@LOG
     log.info("server[" ..
     proxy.global.backends[proxy.connection.backend_ndx].dst.name ..
         proxy.connection.backend = 0
         ---[=[ @@LOG
     elseif auth.packet:byte() == proxy.MYSQLD_PACKET_EOF then
-        -- we received either a 
+        -- we received either a
         -- * MYSQLD_PACKET_ERR and the auth failed or
         -- * MYSQLD_PACKET_EOF which means a OLD PASSWORD (4.0) was sent
         log.debug("  read_auth_result ... not ok yet");
     end
     --]=]
 
-    if cmd.type == proxy.COM_QUIT 
+    if cmd.type == proxy.COM_QUIT
         or cmd.type == proxy.COM_PING then
         -- don't send COM_QUIT to the backend. We manage the connection
         -- in all aspects.
                     end
                 end
 
-                -- if we ask for the last-insert-id we have to ask it on 
+                -- if we ask for the last-insert-id we have to ask it on
                 -- the original connection
                 if not is_insert_id then
-                    idle_ro() 
+                    idle_ro()
                 ---[=[ @@LOG
                 else
                     log.info("found a SELECT LAST_INSERT_ID()," ..
                 --]=]
                 end
 
-                -- prohibit modification of data because we only want to proxy slaves
+            -- prohibit modification of data because we only want to proxy slaves
+            --[[ rw-spliting
             elseif stmt.token_name == "TK_SQL_UPDATE"
                 or stmt.token_name == "TK_SQL_DELETE"
                 or stmt.token_name == "TK_SQL_CREATE"
                 ---[=[ @@LOG
                 log.debug("undefine query, also choose slave")
                 --]=]
-                idle_ro() 
+                idle_ro()
             end
+            --]]
         elseif cmd.type == proxy.COM_INIT_DB then
             -- TODO: @need search or cmd.type == proxy.COM_FIELD_LIST then
-        
             -- init DB can execute on slave
-            idle_ro() 
+
+            idle_ro()
+        end
+        --[[ rw-spliting
         else
             -- prohibit other type
             proxy.response = {
             print("do not supprt type " .. cmd.type_name)
             return proxy.PROXY_SEND_RESULT
         end
-    else 
+        --]]
+    else
         --TODO in transaction
         if proxy.connection.backend_ndx == 0 then
-            print("in transation, but do not have a assigned backend")
-            assert(false)
+            log.error("in transation, but do not have a assigned backend")
+
+            proxy.response = {
+                type = proxy.MYSQLD_PACKET_ERR,
+                errmsg = "in transation, no a assigned backend"
+            }
+            return proxy.PROXY_SEND_RESULT
         end
     end
 
     end
 
     if proxy.connection.backend_ndx == 0 then
-        -- in case the master is down, we have to close the client 
-        -- connections otherwise we can go on
-        ---[=[ @@LOG
+        -- Maybe the master is down
+        -- OR NO AVAILABLE CONNECTIONS
+        -- !!!Very disappointed:( MySQL proxy can not connect server in read_query
+        -- we have to return err packet.
         log.error("query need master, and master is down")
-        log.error("type : " .. cmd.type_name)
-        if cmd.type == proxy.COM_QUERY then
-            log.error("  query              = " .. cmd.query)
-        end
         --]=]
+
+        proxy.response = {
+            type = proxy.MYSQLD_PACKET_ERR,
+            errmsg = "no available connections"
+        }
         return proxy.PROXY_SEND_RESULT
     end
 
     local s = assert(proxy.connection.server)
     -- if client and server db don't match, adjust the server-side
     -- skip it if we send a INIT_DB anyway
-    if cmd.type ~= proxy.COM_INIT_DB and 
+    if cmd.type ~= proxy.COM_INIT_DB and
         c.default_db and c.default_db ~= s.default_db then
         ---[=[ @@LOG
         log.debug("  server default db  = " .. s.default_db)
         log.debug("  is_slave           = " .. tostring(b.type == proxy.BACKEND_TYPE_RO));
         log.debug("  server default db  = " .. s.default_db)
         log.debug("  server username    = " .. s.username)
-    end 
+    end
     log.debug("  server username    = " .. s.username)
     log.debug("  in_trans           = " .. tostring(is_in_transaction))
     log.debug("  in_calc_found      = " .. tostring(is_in_select_calc_found_rows))
 
 function disconnect_client()
     -- close the connections if we have enough connections in the pool
-    -- @return nil           close connection 
+    -- @return nil           close connection
     -- @return IGNORE_RESULT store connection in the pool
 
     ---[=[ @@LOG
                 ---[=[ @@LOG
                 proxy.connection.backend_ndx = index
                 proxy.global.balance.disconnect_index = index + 1
-                log.info("  " .. s.dst.name .. 
+                log.info("  " .. s.dst.name ..
                          " closing connection, idling: " .. cur_idle)
                 --]=]
                 return
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.