為使本說明書的目的、技術方案和優點更加清楚,下面將結合本說明書具體實施例及相應的附圖對本發明技術方案進行清楚、完整地描述。顯然,所描述的實施例僅是本發明一部分實施例,而不是全部的實施例。基於說明書中的實施例,本領域普通技術人員在沒有做出進步性勞動前提下所獲得的所有其他實施例,都屬於本發明保護的範圍。
以下結合附圖,詳細說明本發明各實施例提供的技術方案。
圖1為說明書實施例提供的一種記帳的過程,具體可包括以下步驟:
S100:針對接收到的每個記帳請求,確定該記帳請求針對的帳戶。
在本說明書一個或多個實施例中,可由記帳系統接受記帳請求,並執行後續步驟的操作。其中,該記帳系統可以包括:用於執行記帳操作的伺服器,以及儲存有帳戶資訊以及記帳明細等與記帳相關的資料的資料庫。當然,該記帳系統具體的形式可根據需要配置,例如,該資料庫可以是分散式資料庫,該伺服器也可以是分散式伺服器,本說明書對此不做限定。
為方便描述,後續本說明書以記帳系統為執行該記帳過程的執行主體進行說明。於是,在本說明書中該記帳系統可接收記帳請求。其中,由於記帳是對經濟活動的記錄,是根據業務執行時的記帳需要而生成的,因此記帳系統通常也是接收執行業務的系統發送的記帳請求的。當然,對於該記帳請求具體是如何生成的本說明書不做限定。
而由於一個事務在執行過程中,可能需要生成多個記帳請求,因此不同的記帳請求可能對應於同一個事務。例如,用戶A向用戶B轉帳100元,則執行業務的系統可分別生成從用戶A的帳戶中扣除100元,以及將用戶B的帳戶添加100元的兩個記帳請求。則上述兩個記帳請求時對應於同一個轉帳事務。而為了表明記帳請求對應的事務,通常在生成記帳請求時,會將記帳請求對應的事務的標識攜帶在記帳請求中(例如,記帳請求中可攜帶有流水號、業務標識等等)。
另外,本說明書提供的記帳過程,主要用於解決現有技術在高度並行記帳請求的場景下存在的問題,因此本說明書也已高度並行記帳請求的場景為例進行後續的說明。因此該記帳系統可以接收到若干記帳請求,並且可針對接收到的每個記帳請求,確定該記帳請求針對的帳戶。
具體的,由於在高度並行場景下,記帳系統在同一時間可以接收到複數的記帳請求,而該記帳請求可能是針對不完全相同的帳戶的記帳請求,並且也可能是在執行不完全相同的事務時生成的,因此記帳系統可先確定接收到的每個記帳請求分別針對的帳戶,以便確定後續步驟的操作。
例如,假設記帳系統在某一時刻接收到了如表2所示的記帳請求。
記帳請求
針對的帳戶
對應的事務
內容
記帳請求n
帳戶a
事務001
支出10元
記帳請求n+1
帳戶c
事務001
凍結100元
……
……
……
……
記帳請求m
帳戶f
事務101
收入20元
表2
該記帳系統在同一時間接收到的各記帳請求可以對應不完全相同的帳戶以及不完全相同的事務的。於是,記帳系統可針對每個記帳請求,確定該記帳請求針對的帳戶,以便後續步驟的操作。
S102:當與該記帳請求屬於同一事務的其他記帳請求中,存在針對所述帳戶且待處理的記帳請求時,根據該記帳請求以及所述待處理的記帳請求,判斷是否需要扣除所述帳戶的餘額,若是,則獲取所述帳戶的資源鎖,進行記帳操作,若否,則在不能獲取所述帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存。
在本說明書中,由於步驟S100中針對接收到的每個記帳請求,都確定了該記帳請求針對的帳戶,因此在步驟S102中,仍然基於該記帳請求進行說明。
另外,在本說明書中,根據記帳請求的內容的不同,可將記帳請求對應的記帳操作劃分為4種類型的操作,即,凍結操作、解凍操作、流出操作以及流入操作。
而現有技術中記帳請求對應的記帳操作需要同步執行的原因是:若不同步進行記帳,可能導致事務執行結果出現扣除金額的數值大於帳戶中餘額的數值情況出現,可能導致事務執行出現錯誤或者異常。
於是,在本說明書中,為了避免這一情況的出現,可以根據記帳請求對應的操作類型以及對應的內容,判斷是否需要扣除帳戶的餘額,若是,則採用標準的記帳方式進行記帳操作,若否,則在無法獲取帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存,並在該記帳請求所屬的事務結束之前,進行相應的記帳操作。也就是非同步的進行記帳操作,從解決的高度並行場景下記帳請求對應的記帳操作不執行完畢,事務不會繼續執行而導致各類問題(如,事務等待時間較長,影響效率,或者事務超時導致事務執行失敗等等問題)。
具體的,由於對應不同事務的記帳請求不能一起結算,而針對不同帳戶的記帳請求也不能一起結算,因此記帳系統可根據步驟S100中確定的記帳請求,判斷是否存在與該記帳請求屬於同一事務,且與該記帳請求針對的是同一帳戶的待處理的記帳請求。若是,則執行步驟S102否則執行步驟S104。
在本說明書步驟S102中,針對存在屬於同一事務,且與該記帳請求針對的是同一帳戶的待處理的記帳請求情況進行說明。記帳系統首先,可以確定該記帳請求的記帳明細並儲存,並確定待處理的記帳請求的記帳明細,其中,記帳明細中包括記帳操作的類型以及記帳操作的金額。之後,根據該記帳請求的記帳明細,以及待處理的記帳請求的記帳明細,確定明細總額,最後判斷明細總額的支出是否大於收入。
若是,則需要扣除帳戶的餘額,說明若非同步進行記帳操作,可能導致帳戶餘額出現錯誤,因此記帳系統可根據該記帳請求以及待處理的記帳請求,獲取該帳戶的資源鎖,以進行記帳操作。當然,若是無法獲取該帳戶的資源鎖,則可與標準記帳過程一致,排隊等待獲取鎖。
若否,則說明當前該事務中對於該帳戶的各記帳請求無需扣除帳戶餘額,若非同步進行記帳操作也不回導致帳戶餘額出現錯誤,因此記帳系統可在不能獲取該帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存。當然,若是記帳系統可獲取該帳戶的資源鎖,則可與標準記帳過程一致,根據該記帳請求以及待處理的記帳請求進行記帳操作。
需要說明的是,在本說明書中,記帳系統確定出的待處理的記帳請求可以存在多個。例如,假設某一事物在執行過程中,按照時間先後順序,先後生成了記帳請求1~記帳請求3,分別為:帳戶b餘額增加100元,帳戶b餘額增加50元以及帳戶b餘額支出10元。可見上述3條記帳請求的明細總額為:帳戶b餘額增加140元。如表3所示。
記帳請求
針對的帳戶
對應的事務
記帳明細
明細總額
記帳請求1
帳戶b
事務002
流入操作,100元
增加100元
記帳請求2
帳戶b
事務002
流入操作,50元
增加150元
記帳請求3
帳戶b
事務002
支出操作,10元
增加140元
表3
表2中每行記帳請求對應的明細總額,為當前該帳戶b在事務002中,待處理的記帳請求確定出的明細總額。因此,當記帳系統接收到上述記帳請求1~3時,若都無法獲取帳戶b的資源鎖,可依次將上述3條記帳請求分別作為待處理的記帳請求進行儲存。
進一步假設,該事務在執行過程中,又生成了一個記帳請求4為:帳戶b餘額增加50元,則記帳系統可進一步確定明細總額的支出不大於收入,因此在無法獲取帳戶b的資源鎖時,還可將該記帳請求作為待處理的記帳請求儲存。
假設,在該事務執行過程,生成的記帳請求4為:帳戶b餘額扣除200元,則記帳系統可確定明細總額的支出大於收入,則此時,記帳系統需要獲取帳戶b的資源鎖,並將進行記帳操作。
可見,在本說明書中,只要對應同一事務,且針對同一帳戶的若干記帳請求的明細總額的支出不大於收入,則記帳系統在無法獲取該帳戶的資源鎖時,都可以將記帳請求作為待儲存的記帳請求儲存。是該事務的執行可以繼續進行,而不會應為排隊等待資源鎖而導致事務執行的效率下降。同時,也可減少排隊等待獲取資源鎖的執行緒的數量,減少了資料庫的運行壓力。
S104:當與該記帳請求屬於同一事務的其他記帳請求中,不存在針對所述帳戶的待處理的記帳請求時,根據該記帳請求,判斷是否需要扣除所述帳戶的餘額,若是,則獲取所述帳戶的資源鎖,進行記帳操作,若否,則在不能獲取所述帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存。
在本說明書中,基於前述步驟S102中所述的判斷步驟,若記帳系統可確定與該記帳請求屬於同一事務的其他記帳請求中,不存在針對該帳戶的待處理的記帳請求時,記帳系統可以根據該記帳請求的記帳操作的類型,判斷是否需要獲取該帳戶的資源鎖。由於,各記帳操作的類型中,除了流入操作以外,其餘記帳操作都存在從帳戶餘額中支出金額的情況存在,因此記帳系統在確定沒有待處理的記帳請求是,可以僅根據該記帳請求的記帳操作類型,判斷該記帳操作的類型是否是流入操作以外的其他記帳操作,若是,則與步驟S102中描述一致,記帳系統可採用標記帳的過程進行記帳操作,若否,則記帳系統可以在不能獲取到該帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存。
基於圖1所示的記帳過程,記帳系統在接收到記帳請求時,可根據該記帳請求對應的記帳明細,來判斷是否在不能獲取到帳戶的資源鎖時,非同步進行記帳操作。或者,當存在與該記帳請求同屬一個事務,且針對同一帳戶的待處理的記帳請求時,根據該記帳請求以及待處理的記帳請求的明細總額,判斷是否可以進行非同步記帳操作。使得無需預先對帳戶是否可進行非同步記帳操作進行配置,在減少人工配置帶來的成本以外,也避免了現有緩衝記帳導致的帳戶風險。並且,對於流入操作占多數的帳戶來說,可以減少獲取帳戶的資源鎖的次數,減輕了資料庫的運行壓力。解決了現有各種記帳技術在面對高度並行的記帳請求場景時存在的問題。
另外,在本說明書中,當記帳系統將記帳請求作為待處理的記帳請求進行儲存之後,由於該記帳請求對應的記帳操作未執行,所以在該記帳請所屬的事務結束之前,記帳系統可再次進行判斷。判斷是否仍儲存有屬於該事務的,針對該帳戶的待處理的記帳請求,若是,則獲取該帳戶的資源鎖,並根據已儲存的若干針對所述帳戶的待處理的記帳請求,進行記帳操作,並在記帳操作執行完畢後確定該事務結束。若否,則可直接確定該事務結束。
基於圖1所示的記帳流程以及上述對記帳流程的描述,本說明書對應提供一種記帳的詳細流程圖,如圖2所示。
S200:針對接收到的每個記帳請求,確定該記帳請求針對的帳戶。
S202:判斷是否存在與該記帳請求屬於同一事務,且針對同一帳戶的已儲存的待處理的記帳請求,若是,執行步驟S204,若否執行步驟S206;
S204:根據該記帳請求,以及確定出的待處理的記帳請求,判斷是否需要扣除所述帳戶的餘額,若是則執行步驟S208,若否,則執行步驟S210;
S206:根據該記帳請求,判斷是否需要扣除所述帳戶的餘額,若是則執行步驟S212,若否,則執行步驟S210;
S208:獲取所述帳戶的資源鎖,根據該記帳請求,以及確定出的待處理的記帳請求進行記帳操作;
S210:在不能獲取所述帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存。
S212:獲取所述帳戶的資源鎖,根據該記帳請求進行記帳操作。
進一步地,步驟S210具體還可包括:
S2100:判斷是否獲取到了所述帳戶的資源鎖,若是執行步驟S2102,若否,則執行步驟S2104;
S2102:進行記帳操作;
S2104:將該記帳請求作為待處理的記帳請求進行儲存。如圖3所示。
需要說明的是,本說明書實施例所提供方法的各步驟的執行主體均可以是同一設備,或者,該方法也由不同設備作為執行主體。例如,步驟S100和步驟S102的執行主體可以為設備1,步驟S102的執行主體可以為設備2;或者,步驟S100的執行主體可以為設備1,步驟S102和步驟S104的執行主體可以為設備2;等等。上述對本說明書特定實施例進行了描述。其它實施例在所附申請專利範圍的範圍內。在一些情況下,在申請專利範圍中記載的動作或步驟可以按照不同於實施例中的順序來執行並且仍然可以實現期望的結果。另外,在附圖中描繪的過程不一定要求顯示的特定順序或者連續順序才能實現期望的結果。在某些實施方式中,多工處理和並行處理也是可以的或者可能是有利的。
基於圖1所示的記帳的方法,本說明書實施例還提供一種記帳的裝置,如圖4所示。
圖4為本說明書實施例提供的一種記帳的裝置的結構示意圖,所述裝置包括:
請求確定模組300,針對接收到的每個記帳請求,確定該記帳請求針對的帳戶;
記帳以及儲存模組302,當與該記帳請求屬於同一事務的其他記帳請求中,存在針對所述帳戶的待處理的記帳請求時,根據該記帳請求以及所述待處理的記帳請求,判斷是否需要扣除所述帳戶的餘額,若是,則獲取所述帳戶的資源鎖,進行記帳操作,若否,則在不能獲取所述帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存;
所述記帳以及儲存模組302,當與該記帳請求屬於同一事務的其他記帳請求中,不存在針對所述帳戶的待處理的記帳請求時,根據該記帳請求,判斷是否需要扣除所述帳戶的餘額,若是,則獲取所述帳戶的資源鎖,進行記帳操作,若否,則在不能獲取所述帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存。
所述記帳以及儲存模組302,判斷是否需要扣除所述帳戶的餘額之前,根據該記帳請求,確定記帳明細並儲存,其中所述記帳明細中包括對所述帳戶進行記帳操作的金額。
所述記帳以及儲存模組302,根據儲存的所述待處理的記帳請求的記帳明細,以及該記帳請求的記帳明細,確定該記帳請求以及所述待處理的記帳請求的明細總額,判斷所述明細總額的支出是否大於收入。
所述記帳請求對應的記帳操作的操作類型包括:凍結、解凍、流出以及流入中的一種。
所述記帳以及儲存模組302,確定該記帳請求的操作類型,判斷所述操作類型是否是流入操作類型以外的其他操作類型。
當與該記帳請求屬於同一事務的其他記帳請求中,存在針對所述帳戶的待處理的記帳請求時,所述記帳以及儲存模組302,獲取所述帳戶的資源鎖,並根據該記帳請求以及所述未處理的記帳請求,對所述帳戶進行記帳操作。
當與該記帳請求屬於同一事務的其他記帳請求中,不存在針對所述帳戶的待處理的記帳請求時,所述記帳以及儲存模組302,獲取所述帳戶的資源鎖,並根據該記帳請求對所述帳戶進行記帳操作。
所述記帳以及儲存模組302,當判斷不需要扣除所述帳戶的餘額,且獲取到了所述帳戶的資源鎖時,則根據該記帳請求進行記帳操作,或根據該記帳請求以及所述待處理的記帳請求進行記帳操作。
所述裝置還包括:
補帳模組304,當所述事務結束之前,判斷是否仍儲存有針對所述帳戶的待處理的記帳請求,若是,則獲取所述帳戶的資源鎖,並在根據已儲存的若干針對所述帳戶的待處理的記帳請求,進行記帳操作之後,確定所述事務結束,若否,則確定所述事務結束。
基於圖1所述的記帳的方法,本說明書對應提供一種伺服器,如圖10所示,其中,所述伺服器包括:一個或多個處理器及記憶體,記憶體儲存有程式,並且被配置成由一個或多個處理器執行以下步驟:
針對接收到的每個記帳請求,確定該記帳請求針對的帳戶;
當與該記帳請求屬於同一事務的其他記帳請求中,存在針對所述帳戶的待處理的記帳請求時,根據該記帳請求以及所述待處理的記帳請求,判斷是否需要扣除所述帳戶的餘額,若是,則獲取所述帳戶的資源鎖,進行記帳操作,若否,則在不能獲取所述帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存;
當與該記帳請求屬於同一事務的其他記帳請求中,不存在針對所述帳戶的待處理的記帳請求時,根據該記帳請求,判斷是否需要扣除所述帳戶的餘額,若是,則獲取所述帳戶的資源鎖,進行記帳操作,若否,則在不能獲取所述帳戶的資源鎖時,將該記帳請求作為待處理的記帳請求進行儲存。
在20世紀90年代,對於一個技術的改進可以很明顯地區分是硬體上的改進(例如,對二極體、電晶體、開關等電路結構的改進)還是軟體上的改進(對於方法流程的改進)。然而,隨著技術的發展,當今的很多方法流程的改進已經可以視為硬體電路結構的直接改進。設計人員幾乎都藉由將改進的方法流程程式設計到硬體電路中來得到相應的硬體電路結構。因此,不能說一個方法流程的改進就不能用硬體實體模組來實現。例如,可程式設計邏輯裝置(Programmable Logic Device, PLD)(例如現場可程式設計閘陣列(Field Programmable Gate Array,FPGA))就是這樣一種積體電路,其邏輯功能由用戶對裝置程式設計來確定。由設計人員自行程式設計來把一個數位系統“整合”在一片PLD上,而不需要請晶片製造廠商來設計和製作專用的積體電路晶片。而且,如今,取代手工地製作積體電路晶片,這種程式設計也多半改用“邏輯編譯器(logic compiler)”軟體來實現,它與程式開發撰寫時所用的軟體編譯器相類似,而要編譯之前的原始代碼也得用特定的程式設計語言來撰寫,此稱之為硬體描述語言(Hardware Description Language,HDL),而HDL也並非僅有一種,而是有許多種,如ABEL(Advanced Boolean Expression Language)、AHDL(Altera Hardware Description Language)、Confluence、CUPL(Cornell University Programming Language)、HDCal、JHDL(Java Hardware Description Language)、Lava、Lola、MyHDL、PALASM、RHDL(Ruby Hardware Description Language)等,目前最普遍使用的是VHDL(Very-High-Speed Integrated Circuit Hardware Description Language)與Verilog。本領域技術人員也應該清楚,只需要將方法流程用上述幾種硬體描述語言稍作邏輯程式設計並程式設計到積體電路中,就可以很容易得到實現該邏輯方法流程的硬體電路。
控制器可以按任何適當的方式實現,例如,控制器可以採取例如微處理器或處理器以及儲存可由該(微)處理器執行的電腦可讀程式碼(例如軟體或韌體)的電腦可讀媒體、邏輯閘、開關、特殊應用積體電路(Application Specific Integrated Circuit,ASIC)、可程式設計邏輯控制器和嵌入微控制器的形式,控制器的例子包括但不限於以下微控制器:ARC 625D、Atmel AT91SAM、Microchip PIC18F26K20以及Silicone Labs C8051F320,記憶體控制器還可以被實現為記憶體的控制邏輯的一部分。本領域技術人員也知道,除了以純電腦可讀程式碼方式實現控制器以外,完全可以藉由將方法步驟進行邏輯程式設計來使得控制器以邏輯閘、開關、特殊應用積體電路、可程式設計邏輯控制器和嵌入微控制器等的形式來實現相同功能。因此這種控制器可以被認為是一種硬體部件,而對其內包括的用於實現各種功能的裝置也可以視為硬體部件內的結構。或者甚至,可以將用於實現各種功能的裝置視為既可以是實現方法的軟體模組又可以是硬體部件內的結構。
上述實施例闡明的系統、裝置、模組或單元,具體可以由電腦晶片或實體實現,或者由具有某種功能的產品來實現。一種典型的實現設備為電腦。具體的,電腦例如可以為個人電腦、膝上型電腦、蜂巢式電話、相機電話、智慧型電話、個人數位助理、媒體播放機、導航設備、電子郵件設備、遊戲控制台、平板電腦、可穿戴設備或者這些設備中的任何設備的組合。
為了描述的方便,描述以上裝置時以功能分為各種單元分別描述。當然,在實施本發明時可以把各單元的功能在同一個或多個軟體和/或硬體中實現。
本領域內的技術人員應明白,本發明的實施例可提供為方法、系統、或電腦程式產品。因此,本發明可採用完全硬體實施例、完全軟體實施例、或結合軟體和硬體態樣的實施例的形式。而且,本發明可採用在一個或多個其中包含有電腦可用程式碼的電腦可用儲存媒體(包括但不限於磁碟記憶體、CD-ROM、光學記憶體等)上實施的電腦程式產品的形式。
本發明是參照根據本發明實施例的方法、設備(系統)、和電腦程式產品的流程圖和/或方塊圖來描述的。應理解可由電腦程式指令實現流程圖和/或方塊圖中的每一流程和/或方塊、以及流程圖和/或方塊圖中的流程和/或方塊的結合。可提供這些電腦程式指令到通用電腦、專用電腦、嵌入式處理機或其他可程式設計資料處理設備的處理器以產生一個機器,使得藉由電腦或其他可程式設計資料處理設備的處理器執行的指令產生用於實現在流程圖一個流程或多個流程和/或方塊圖一個方塊或多個方塊中指定的功能的裝置。
這些電腦程式指令也可儲存在能引導電腦或其他可程式設計資料處理設備以特定方式工作的電腦可讀記憶體中,使得儲存在該電腦可讀記憶體中的指令產生包括指令裝置的製造品,該指令裝置實現在流程圖一個流程或多個流程和/或方塊圖一個方塊或多個方塊中指定的功能。
這些電腦程式指令也可裝載到電腦或其他可程式設計資料處理設備上,使得在電腦或其他可程式設計設備上執行一系列操作步驟以產生電腦實現的處理,從而在電腦或其他可程式設計設備上執行的指令提供用於實現在流程圖一個流程或多個流程和/或方塊圖一個方塊或多個方塊中指定的功能的步驟。
在一個典型的配置中,計算設備包括一個或多個處理器(CPU)、輸入/輸出介面、網路介面和記憶體。
記憶體可能包括電腦可讀媒體中的非永久性記憶體,隨機存取記憶體(RAM)和/或非揮發性記憶體等形式,如唯讀記憶體(ROM)或快閃記憶體(flash RAM)。記憶體是電腦可讀媒體的示例。
電腦可讀媒體包括永久性和非永久性、可移動和非可移動媒體可以由任何方法或技術來實現資訊儲存。資訊可以是電腦可讀指令、資料結構、程式的模組或其他資料。電腦的儲存媒體的例子包括,但不限於相變記憶體(PRAM)、靜態隨機存取記憶體(SRAM)、動態隨機存取記憶體(DRAM)、其他類型的隨機存取記憶體(RAM)、唯讀記憶體(ROM)、電可抹除可程式設計唯讀記憶體(EEPROM)、快閃記憶體或其他記憶體技術、唯讀光碟唯讀記憶體(CD-ROM)、數位多功能光碟(DVD)或其他光學儲存、磁盒式磁帶,磁帶磁磁片儲存或其他磁性存放裝置或任何其他非傳輸媒體,可用於儲存可以被計算設備存取的資訊。按照本文中的界定,電腦可讀媒體不包括暫存電腦可讀媒體(transitory media),如調變的資料訊號和載波。
還需要說明的是,術語“包括”、“包含”或者其任何其他變體意在涵蓋非排他性的包含,從而使得包括一系列要素的過程、方法、商品或者設備不僅包括那些要素,而且還包括沒有明確列出的其他要素,或者是還包括為這種過程、方法、商品或者設備所固有的要素。在沒有更多限制的情況下,由語句“包括一個……”限定的要素,並不排除在包括所述要素的過程、方法、商品或者設備中還存在另外的相同要素。
本領域技術人員應明白,本發明的實施例可提供為方法、系統或電腦程式產品。因此,本發明可採用完全硬體實施例、完全軟體實施例或結合軟體和硬體態樣的實施例的形式。而且,本發明可採用在一個或多個其中包含有電腦可用程式碼的電腦可用儲存媒體(包括但不限於磁碟記憶體、CD-ROM、光學記憶體等)上實施的電腦程式產品的形式。
本發明可以在由電腦執行的電腦可執行指令的一般上下文中描述,例如程式模組。一般地,程式模組包括執行特定任務或實現特定抽象資料類型的常式、程式、物件、元件、資料結構等等。也可以在分散式運算環境中實踐本發明,在這些分散式運算環境中,由藉由通訊網路而被連接的遠端處理設備來執行任務。在分散式運算環境中,程式模組可以位於包括存放裝置在內的本地和遠端電腦儲存媒體中。
本說明書中的各個實施例均採用遞進的方式描述,各個實施例之間相同相似的部分互相參見即可,每個實施例重點說明的都是與其他實施例的不同之處。尤其,對於系統實施例而言,由於其基本相似於方法實施例,所以描述的比較簡單,相關之處參見方法實施例的部分說明即可。
以上所述僅為本發明的實施例而已,並不用於限制本發明。對於本領域技術人員來說,本發明可以有各種更改和變化。凡在本發明的精神和原理之內所作的任何修改、等同替換、改進等,均應包含在本發明的申請專利範圍的範圍之內。
In order to make the purpose, technical solutions and advantages of the description clearer, the technical solutions of the present invention will be described clearly and completely in conjunction with the specific embodiments of the description and the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, but not all the embodiments. Based on the embodiments in the specification, all other embodiments obtained by a person of ordinary skill in the art without making progressive labor fall within the protection scope of the present invention. The technical solutions provided by the embodiments of the present invention will be described in detail below with reference to the drawings. FIG. 1 is a billing process provided by an embodiment of the specification, and may specifically include the following steps: S100: For each billing request received, determine an account to which the billing request is directed. In one or more embodiments of this specification, the billing system may accept the billing request and perform the operations of the subsequent steps. Wherein, the accounting system may include: a server for performing accounting operations, and a database storing accounting-related data such as account information and accounting details. Of course, the specific form of the accounting system can be configured as required. For example, the database can be a distributed database, and the server can also be a distributed server, which is not limited in this specification. For the convenience of description, the following description will use the accounting system as the main body to execute the accounting process. Thus, in this specification, the billing system can receive billing requests. Among them, since accounting is a record of economic activity, which is generated according to the accounting needs when the business is executed, the accounting system usually also receives the accounting request sent by the system that performs the business. Of course, this specification does not limit how the billing request is specifically generated. Since a transaction may need to generate multiple accounting requests during execution, different accounting requests may correspond to the same transaction. For example, if user A transfers 100 yuan to user B, the system that performs the business may generate two billing requests that deduct 100 yuan from user A's account and add 100 yuan to user B's account. Then the above two billing requests correspond to the same transfer transaction. In order to indicate the transaction corresponding to the billing request, usually when generating the billing request, the identifier of the transaction corresponding to the billing request is carried in the billing request (for example, the billing request may carry a serial number, business identifier, etc.). In addition, the billing process provided in this specification is mainly used to solve the problems existing in the scenario of highly parallel billing requests in the prior art. Therefore, this specification has also used the scene of highly parallel billing requests as an example for subsequent description. Therefore, the billing system can receive several billing requests, and for each billing request received, the account to which the billing request is directed can be determined. Specifically, in a highly parallel scenario, the billing system can receive multiple billing requests at the same time, and the billing request may be a billing request for accounts that are not exactly the same, and may also be performing different transactions. Generated at the time, so the billing system can first determine the account for each billing request received in order to determine the operation of the subsequent steps. For example, assume that the billing system receives the billing request shown in Table 2 at a certain moment. Billing request Targeted account Corresponding transaction content
Accounting request n Account a Transaction 001 Pay 10 yuan
Accounting request n+1 Account c Transaction 001 Freeze 100 yuan
... ... ... ...
Accounting request m Account f Transaction 101 Income 20 yuan
Table 2 The billing requests received by the billing system at the same time can correspond to incomplete accounts and incomplete transactions. Therefore, the billing system can determine the account to which the billing request is directed for each billing request, so as to facilitate the operation of the subsequent steps. S102: When there is a billing request that is pending for the account among other billing requests that belong to the same transaction as the billing request, it is determined whether the account needs to be deducted based on the billing request and the pending billing request The balance, if it is, acquires the resource lock of the account to perform the accounting operation, and if not, stores the accounting request as a pending accounting request when the resource lock of the account cannot be acquired. In this specification, since each accounting request received in step S100 determines the account to which the accounting request is directed, in step S102, the explanation is still based on the accounting request. In addition, in this specification, according to the different contents of the billing request, the billing operation corresponding to the billing request can be divided into four types of operations, namely, freezing operation, unfreezing operation, outflow operation, and inflow operation. The reason why the accounting operation corresponding to the accounting request in the prior art needs to be performed synchronously is: if the accounting is not performed synchronously, the transaction execution result may cause the value of the deduction amount to be greater than the value of the balance in the account, which may cause an error or exception in the transaction execution . Therefore, in this specification, in order to avoid this situation, you can determine whether the balance of the account needs to be deducted according to the operation type and corresponding content corresponding to the accounting request. If it is, then use the standard accounting method to perform the accounting operation. , When the resource lock of the account cannot be obtained, the accounting request is stored as a pending accounting request, and the corresponding accounting operation is performed before the transaction to which the accounting request belongs ends. That is, the accounting operation is performed asynchronously, and the accounting operation corresponding to the accounting request is not completed from the highly parallel scenario solved, and the transaction will not continue to be executed, resulting in various problems (such as long transaction waiting time, affecting efficiency, or transaction (Timeout causes transaction execution failure and so on). Specifically, since the billing requests corresponding to different transactions cannot be settled together, and the billing requests for different accounts cannot be settled together, the billing system can determine whether there is the same transaction as the billing request according to the billing request determined in step S100, And the billing request is for a pending billing request of the same account. If yes, step S102 is executed; otherwise, step S104 is executed. In step S102 of the present specification, the case where there is a pending billing request that belongs to the same transaction and is directed to the same account as the billing request is described. The billing system can first determine and store the billing details of the billing request, and determine the billing details of the pending billing request. The billing details include the type of billing operation and the amount of billing operation. After that, according to the billing details of the billing request and the billing details of the pending billing request, the total amount of the detail is determined, and finally whether the expenditure of the total amount of the detail is greater than the income is determined. If yes, the account balance needs to be deducted, indicating that if the accounting operation is not performed synchronously, the account balance may be wrong. Therefore, the accounting system can obtain the resource lock of the account according to the accounting request and the pending accounting request to perform the accounting operation. Of course, if it is impossible to obtain the resource lock of the account, it can be in line with the standard accounting process, waiting in line to obtain the lock. If not, it means that there is no need to deduct the account balance for each billing request for the account in the current transaction. If the billing operation is not performed synchronously, the account balance will be wrong, so the billing system can remove the account’s resource lock when The accounting request is stored as a pending accounting request. Of course, if the accounting system can obtain the resource lock of the account, it can be consistent with the standard accounting process, and the accounting operation can be performed according to the accounting request and the pending accounting request. It should be noted that, in this specification, there may be multiple billing requests determined by the billing system to be processed. For example, suppose that during the execution of a thing, in accordance with the chronological order, accounting request 1 ~ accounting request 3 are generated, respectively: account b balance increases by 100 yuan, account b balance increases by 50 yuan, and account b balance spends 10 yuan . It can be seen that the total amount of detail of the above three accounting requests is: the balance of account b is increased by 140 yuan. as shown in Table 3. Billing request Targeted account Corresponding transaction Billing details Total breakdown
Accounting request 1 Account b Transaction 002 Inflow operation, 100 yuan $100 increase
Accounting request 2 Account b Transaction 002 Inflow operation, 50 yuan 150 yuan increase
Accounting request 3 Account b Transaction 002 Expenditure operation, 10 yuan Increase by 140 yuan
Table 3 The total amount of detail corresponding to the accounting request for each line in Table 2 is the total amount of detail determined for the current accounting request of account b in transaction 002. Therefore, when the billing system receives the above billing requests 1 to 3, if none of the resource locks of the account b can be obtained, the three billing requests can be sequentially stored as pending billing requests. Further suppose that during the execution of the transaction, an accounting request 4 was generated as follows: the balance of account b increased by 50 yuan, then the accounting system can further determine that the total expenditure of the detail is not greater than the income, so when the resource lock of account b cannot be obtained , You can also store the billing request as a pending billing request. Suppose, during the execution of the transaction, the generated accounting request 4 is: the account b balance is deducted by 200 yuan, then the accounting system can determine that the total amount of expenditure is greater than the income, then at this time, the accounting system needs to obtain the resource lock of account b, and Perform accounting operations. It can be seen that, in this specification, as long as it corresponds to the same transaction, and the total amount of expenditures for several accounting requests for the same account is not greater than income, the accounting system can use the accounting request as pending storage when it cannot obtain the resource lock for the account 'S accounting request is stored. It is because the execution of the transaction can continue, and the efficiency of the transaction execution will not decrease because of waiting in line for the resource lock. At the same time, it can also reduce the number of threads waiting in line to obtain resource locks, reducing the operating pressure of the database. S104: When there is no pending billing request for the account among other billing requests that belong to the same transaction as the billing request, according to the billing request, it is determined whether the balance of the account needs to be deducted. The resource lock of the account is used to perform the accounting operation. If not, when the resource lock of the account cannot be obtained, the accounting request is stored as a pending accounting request. In this specification, based on the judgment step described in the foregoing step S102, if the billing system can determine that other billing requests that belong to the same transaction as the billing request, there is no pending billing request for the account, the billing system can According to the type of billing operation of the billing request, it is determined whether the resource lock of the account needs to be acquired. As for each type of billing operation, except for the inflow operation, there are cases where the amount of money is spent from the account balance in other billing operations, so the billing system determines that there are no pending billing requests, and can only bill based on the billing request Operation type, to determine whether the type of the accounting operation is other than the inflow operation. If it is, it is consistent with the description in step S102. The accounting system can use the process of marking the account to perform the accounting operation. If not, the accounting system can When the resource lock of the account is obtained, the accounting request is stored as a pending accounting request. Based on the billing process shown in FIG. 1, when the billing system receives the billing request, it can determine whether to perform the billing operation asynchronously when the resource lock of the account cannot be obtained according to the billing details corresponding to the billing request. Or, when there is a pending billing request that belongs to the same transaction as the billing request and is for the same account, it is determined whether the asynchronous billing operation can be performed according to the total amount of details of the billing request and the pending billing request. This eliminates the need to configure in advance whether the account can perform asynchronous accounting operations. In addition to reducing the cost of manual configuration, it also avoids the account risk caused by the existing buffer accounting. In addition, for accounts with a majority of inflow operations, the number of resource locks for the account can be reduced and the operating pressure of the database can be reduced. It solves the problems existing in the existing various billing technologies when facing highly parallel billing request scenarios. In addition, in this specification, after the billing system stores the billing request as a pending billing request, since the billing operation corresponding to the billing request has not been performed, the billing system can perform again before the transaction to which the billing belongs belongs ends judgment. Determine whether there is still a pending billing request for the account that belongs to the transaction, and if so, acquire the resource lock for the account and perform the billing operation based on the stored pending billing requests for the account , And determine the end of the transaction after the accounting operation is completed. If not, you can directly determine the end of the transaction. Based on the accounting process shown in FIG. 1 and the above description of the accounting process, this specification provides a detailed flowchart of accounting, as shown in FIG. 2. S200: For each billing request received, determine the account to which the billing request is directed. S202: determine whether there is a pending accounting request that belongs to the same transaction as the accounting request and is for the same account, if so, step S204 is executed, and if not, step S206 is executed; S204: according to the accounting request and the determined The pending accounting request determines whether the balance of the account needs to be deducted, if yes, step S208 is executed, and if not, step S210 is executed; S206: According to the accounting request, whether the balance of the account needs to be deducted is determined, if yes Step S212, if not, step S210 is executed; S208: acquiring the resource lock of the account, performing accounting operations according to the accounting request and the determined pending accounting request; S210: unable to acquire the resource lock of the account At this time, the accounting request is stored as a pending accounting request. S212: Acquire a resource lock of the account, and perform an accounting operation according to the accounting request. Further, step S210 may further specifically include: S2100: determine whether the resource lock of the account is obtained, if step S2102 is executed, if not, step S2104 is executed; S2102: perform accounting operation; S2104: perform the accounting request as pending The processed accounting request is stored. As shown in Figure 3. It should be noted that, the execution body of each step of the method provided in the embodiments of the present specification may be the same device, or the method may also be performed by different devices. For example, the execution subject of step S100 and step S102 may be device 1, and the execution subject of step S102 may be device 2; or, the execution subject of step S100 may be device 1, and the execution subject of step S102 and step S104 may be device 2; and many more. The foregoing describes specific embodiments of the present specification. Other embodiments are within the scope of the attached patent application. In some cases, the actions or steps described in the scope of the patent application may be performed in a different order than in the embodiment and still achieve the desired result. In addition, the processes depicted in the drawings do not necessarily require the particular order shown or sequential order to achieve the desired results. In some embodiments, multiplexing and parallel processing are also possible or may be advantageous. Based on the billing method shown in FIG. 1, an embodiment of this specification further provides a billing device, as shown in FIG. 4. 4 is a schematic structural diagram of a billing device provided by an embodiment of the present specification. The device includes: a request determination module 300, for each billing request received, the account to which the billing request is directed is determined; a billing and storage module 302. When there is a pending billing request for the account among other billing requests belonging to the same transaction as the billing request, determine whether the account needs to be deducted based on the billing request and the pending billing request The balance, if it is, acquire the resource lock of the account and perform the accounting operation; if not, store the accounting request as a pending accounting request when the resource lock of the account cannot be acquired; the accounting and storage Module 302, when there is no pending billing request for the account among other billing requests that belong to the same transaction as the billing request, according to the billing request, it is determined whether the balance of the account needs to be deducted, and if so, then Acquire the resource lock of the account and perform the accounting operation. If not, when the resource lock of the account cannot be acquired, store the accounting request as a pending accounting request. Before determining whether it is necessary to deduct the balance of the account, the billing and storage module 302 determines and stores the billing details according to the billing request, where the billing details include the amount of billing operations performed on the account. The accounting and storage module 302 determines the total amount of details of the accounting request and the pending accounting request based on the stored accounting details of the pending accounting request and the accounting details of the accounting request, and determines the Whether the total expenditure is greater than the income. The operation type of the accounting operation corresponding to the accounting request includes one of freezing, thawing, outflow, and inflow. The accounting and storage module 302 determines the operation type of the accounting request and determines whether the operation type is an operation type other than the inflow operation type. When there is a pending billing request for the account among other billing requests that belong to the same transaction as the billing request, the billing and storage module 302 obtains the resource lock of the account, and according to the billing request and The unprocessed billing request performs a billing operation on the account. When there is no pending billing request for the account in other billing requests that belong to the same transaction as the billing request, the billing and storage module 302 obtains the resource lock of the account and based on the billing request Perform a billing operation on the account. The accounting and storage module 302, when it is determined that the balance of the account does not need to be deducted, and the resource lock of the account is acquired, then performs an accounting operation according to the accounting request, or according to the accounting request and the pending Accounting request for accounting operations. The device further includes: a replenishment account module 304, which determines whether the pending accounting request for the account is still stored before the end of the transaction, and if so, acquires the resource lock of the account and checks After storing a number of pending accounting requests for the account after the accounting operation, it is determined that the transaction ends, and if not, it is determined that the transaction ends. Based on the accounting method described in FIG. 1, this specification correspondingly provides a server, as shown in FIG. 10, wherein the server includes: one or more processors and a memory, the memory stores a program, and is It is configured that one or more processors perform the following steps: For each accounting request received, determine the account to which the accounting request is directed; When other accounting requests that belong to the same transaction as the accounting request exist, there are When a billing request is to be processed, according to the billing request and the billing request to be processed, it is determined whether the balance of the account needs to be deducted. If so, the resource lock of the account is acquired and the billing operation is performed. When the resource lock of the account cannot be obtained, the accounting request is stored as a pending accounting request; when there is no pending accounting request for the account in other accounting requests that belong to the same transaction as the accounting request , According to the accounting request, determine whether the balance of the account needs to be deducted, if so, acquire the resource lock of the account and perform the accounting operation, if not, then request the accounting when the resource lock of the account cannot be obtained Store as a pending billing request. In the 1990s, the improvement of a technology can be clearly distinguished from the improvement of hardware (for example, the improvement of the circuit structure of diodes, transistors, switches, etc.) or the improvement of software (for the process flow Improve). However, with the development of technology, the improvement of many methods and processes can be regarded as a direct improvement of the hardware circuit structure. Designers almost get the corresponding hardware circuit structure by programming the improved method flow into the hardware circuit. Therefore, it cannot be said that the improvement of a method and process cannot be achieved with hardware physical modules. For example, a programmable logic device (Programmable Logic Device, PLD) (such as a field programmable gate array (Field Programmable Gate Array, FPGA)) is such an integrated circuit, and its logic function is determined by the user programming the device. It is up to the designer to program a digital system to "integrate" a PLD without having to ask a chip manufacturer to design and manufacture a dedicated integrated circuit chip. Moreover, nowadays, instead of manually manufacturing integrated circuit chips, this kind of programming is also mostly implemented with "logic compiler" software, which is similar to the software compiler used in program development and writing. The original code before compilation must also be written in a specific programming language, which is called the hardware description language (Hardware Description Language, HDL), and HDL is not only one, but there are many, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc., Currently the most commonly used are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art should also understand that it is easy to obtain the hardware circuit that implements the logic method flow by only slightly programming the method flow using the above hardware description languages and programming it into the integrated circuit. The controller can be implemented in any suitable manner, for example, the controller can take, for example, a microprocessor or processor and a computer-readable program code (such as software or firmware) that can be executed by the (micro)processor Media, logic gates, switches, application specific integrated circuits (ASICs), programmable logic controllers, and embedded microcontrollers. Examples of controllers include but are not limited to the following microcontrollers: ARC 625D , Atmel AT91SAM, Microchip PIC18F26K20 and Silicone Labs C8051F320, the memory controller can also be implemented as part of the control logic of the memory. Those skilled in the art also know that, in addition to implementing the controller in a pure computer-readable code, the logic steps can be completely programmed by the method steps to make the controller with logic gates, switches, integrated circuits for special applications, and programmable Logic controller and embedded microcontroller to achieve the same function. Therefore, such a controller can be regarded as a hardware component, and the device for implementing various functions included therein can also be regarded as a structure within the hardware component. Or even, the device for realizing various functions can be regarded as both a software module of the implementation method and a structure in the hardware component. The system, device, module or unit explained in the above embodiments may be implemented by a computer chip or entity, or by a product with a certain function. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, and a wearable Device or any combination of these devices. For the convenience of description, when describing the above device, the functions are divided into various units and described separately. Of course, when implementing the present invention, the functions of each unit can be implemented in one or more software and/or hardware. Those skilled in the art should understand that the embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can take the form of computer program products implemented on one or more computer usable storage media (including but not limited to disk memory, CD-ROM, optical memory, etc.) containing computer usable program code . The present invention is described with reference to flowcharts and/or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each flow and/or block in the flowchart and/or block diagram and a combination of the flow and/or block in the flowchart and/or block diagram can be implemented by computer program instructions. These computer programming instructions can be provided to the processor of a general purpose computer, dedicated computer, embedded processor, or other programmable data processing device to produce a machine that is executed by the processor of the computer or other programmable data processing device The instruction generation means for realizing the function specified in one block or multiple blocks of one flow or multiple flows of the flowchart and/or one block of the block diagram. These computer program instructions can also be stored in a computer readable memory that can guide a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce a manufactured product including an instruction device The instruction device implements the functions specified in one block or multiple blocks in one flow or multiple processes in the flowchart and/or one block in the block diagram. These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operating steps can be performed on the computer or other programmable device to generate computer-implemented processing, and thus on the computer or other programmable device The instructions executed on provide steps for implementing the functions specified in one block or multiple blocks of the flowchart one flow or multiple flows and/or block diagrams. In a typical configuration, the computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory. Memory may include non-permanent memory, random access memory (RAM) and/or non-volatile memory in computer-readable media, such as read-only memory (ROM) or flash memory (flash) RAM). Memory is an example of computer-readable media. Computer-readable media, including permanent and non-permanent, removable and non-removable media, can be stored by any method or technology. The information can be computer readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM) , Read-only memory (ROM), electrically erasable and programmable read-only memory (EEPROM), flash memory or other memory technologies, read-only disc read-only memory (CD-ROM), digital multifunction Optical discs (DVDs) or other optical storage, magnetic cassette tapes, magnetic tape magnetic tape storage or other magnetic storage devices or any other non-transmission media can be used to store information that can be accessed by computing devices. As defined in this article, computer-readable media does not include temporary computer-readable media (transitory media), such as modulated data signals and carrier waves. It should also be noted that the terms "include", "include" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device that includes a series of elements includes not only those elements, but also includes Other elements not explicitly listed, or include elements inherent to this process, method, commodity, or equipment. Without more restrictions, the element defined by the sentence "include one..." does not exclude that there are other identical elements in the process, method, commodity, or equipment that includes the element. Those skilled in the art should understand that the embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can take the form of computer program products implemented on one or more computer usable storage media (including but not limited to disk memory, CD-ROM, optical memory, etc.) containing computer usable program code . The invention can be described in the general context of computer executable instructions executed by a computer, such as a program module. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present invention can also be practiced in distributed computing environments in which tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices. The embodiments in this specification are described in a progressive manner. The same or similar parts between the embodiments can be referred to each other. Each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant part can be referred to the description of the method embodiment. The above are only the embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and changes. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the scope of the patent application of the present invention.